Reputation: 11480
stringi = "fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd"
I was able to make a regEx to DYNAMICALLY find every last occurrence of a character X in every word: (I call it dynamically because I only have to change ONE spot)
(n)(?!(\w+?)?\1(\w+?)?)
gives
"fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd"
^ ^ ^ ^ ^
(d)(?!(\w+?)?\1(\w+?)?)
gives
"fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd"
^ ^ ^ ^ ^
How can I get a "dynamic" regEx. That means I have to replace one spot like above to give me:
some regEx with >>> n <<<
gives
"fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd"
^ ^ ^ ^ ^
some regEx with >>> d <<<
gives
"fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd"
^ ^ ^ ^ ^
Is it possible to match those with a regEx and that I only have to change ONE spot when I want to match another character?
Pure regEx, no js string splitting etc..
With the lookbehind I run into a fixed length problem.
Upvotes: 0
Views: 101
Reputation: 48711
That means I have to replace one spot
You first match n
then every thing else:
n(\S*)
var str = "fuunnynsdfdn dfdfomdfdnd dfdfnntr ndfdf thatnfdfdfdfd";
var char = 'n';
console.log(str.replace(new RegExp(char + '(\\S*)', 'g'), '*$1'));
var char = 'd';
console.log(str.replace(new RegExp(char + '(\\S*)', 'g'), '*$1'));
If it's a matter of interest, since Chrome has implemented lookbehinds (that luckily are infnite lookbehinds) you can acheive same result with this regex:
(?<!n\S*)n
Upvotes: 2
Reputation: 7166
In this regex demo, the lookbehind seems to be okay with not fixed length - with javascript. So this is the regex:
(?<=\b[^\Wd]*)d
Explanation:
\b
is a word barrier[^d]*d
finds non-d
characters, and then a d
[^\Wd]
is not a d
but word characters. I.e. \W
is not a word character, so [^\W]
is a word character. So [^\Wd]
is every word character, except d
\b[^\Wd]*d
would match the start of every word, up to the first d
. Using the right constructs, this could be enough. (?<=...)
is the lookbehind.Upvotes: 1