RegEx to match all occurrences of a character *after* the first occurrence?
Your problem is that you'd need not just lookbehind (which some regex flavors don't support at all) but infinite repetition inside the lookbehind assertion (which only very few flavors support, like .NET for example).
Therefore, the .NET- or JGSoft-compatible regex
(?<=w.*)w
won't work in Objective C, Python, Java, etc.
So you need to do it in several steps: First find the string after the first w
:
(?<=w).*
(if you have lookbehind support at all), then take the match and search for w
inside that.
If lookbehind isn't supported, then search for w(.*)
and apply the following search to the contents of the capturing group 1 ($1
).
RegEx: Match nth occurrence
You can use this regex:
\b_name=(?:[^"]*"){3}
RegEx Demo
RegEx Details:
\b_name
: Match full word_name
:=
: Match a=
(?:[^"]*"){3}
: Match 0 or more non-"
characters followed by a"
. Repeat this group 3 times.
Finding the nth occurrence of a number using regex
You can use this regex to pick N
th number:
(?:\D*(\d+)){2}
Replace 2
by any number you want. Your number is available in captured group #1
\D
matches any non-digit\d
matches a digit
RegEx Demo
regex to match substring after nth occurence of pipe character
To match part after nth
occurrence of pipe you can use this regex:
/^(?:[^|]*\|){3}([^|]*)/
Here n=3
It will match 10.15.194.25
in matched group #1
RegEx Demo
How to find and replace every nth character occurrence using regex?
You can use:
str = str.replace(/((?:[^x]*x){2}[^x]*)x/g, '$1y');
So if you are searching for every 3rd occurrence then use n-1
between {}
in above regex.
To build it dynamically use:
let str = 'I amx nexver axt hoxme on Sxundxaxs';let n = 3;let ch = 'x';
let regex = new RegExp("((?:[^" +ch+ "]*" +ch+ "){" + (n-1) + "}[^" +ch+ "]*)" +ch, "g");
str = str.replace(regex, '$1y');
console.log(str);//=> I amx nexver ayt hoxme on Sxundyaxs
Match all after nth occurrence of specified character
I would propose this:
[^"]*(?=";)
This only matches the part within quotes, excluding (but requiring) the ending ";
.
See demo on regex101
regular expression capture nth match
for nth match use this pattern (?:.*?\$[0-9.]+){XX}.*?(\$[0-9.]+)
where XX = n-1
Example for 3rd match
regular expression to match everything until the last occurrence of /
You can match this:
.*\/
and replace with your text.
DEMO
Javascript Regex match everything after last occurrence of string
One option would be to match everything up until the last [/quote]
, and then get anything following it. (example)
/.*\[\/quote\](.*)$/i
This works since .*
is inherently greedy, and it will match every up until the last \[\/quote\]
.
Based on the string you provided, this would be the first capturing group match:
\nThis is all the text I\'m wirting about myself.\n\nLook at me ma. Javascript.
But since your string contains new lines, and .
doesn't match newlines, you could use [\s\S]
in place of .
in order to match anything.
Updated Example
/[\s\S]*\[\/quote\]([\s\S]*)$/i
You could also avoid regex and use the .lastIndexOf()
method along with .slice()
:
Updated Example
var match = '[\/quote]';
var textAfterLastQuote = str.slice(str.lastIndexOf(match) + match.length);
document.getElementById('res').innerHTML = "Results: " + textAfterLastQuote;
Alternatively, you could also use .split()
and then get the last value in the array:
Updated Example
var textAfterLastQuote = str.split('[\/quote]').pop();
document.getElementById('res').innerHTML = "Results: " + textAfterLastQuote;
Related Topics
To Check If a String Is Alphanumeric in JavaScript
Bootstrap Modal Only Showing Backdrop
How to Do Validation for 10 Digit Mobile Number Using Jquery
How to Open a Web Page Automatically in Full Screen Mode
Remove Parameter from Url Via Regex
Does JavaScript Guarantee Object Property Order
How to Check If Value Is in Array With Angularjs
How to Add Space Between Every 4 Characters in JavaScript
Programmatically Disabling Chrome Auto-Fill
Passing Variable from Button to Modal
How to Send One Alert from My Node.Js to My JavaScript Client
Focus Next Input Once Reaching Maxlength Value
Create Clone of Table Row and Append to Table in JavaScript
Check If Value Select2 Is Null
How to Get a File() or Blob() from an Url in JavaScript
How to Detect Chinese Character With Punctuation in Regex
Prevent Bootstrap Modal Window from Closing on Form Submission
How to Handle Multiple Submit Buttons in a Form Using Angular Js