Javascript Look

I am trying to imitate the look in Javascript,

I want to match the string "object.all", but not "object.call". I tried:

new RegExp('(?!(\\.))all') 

But both examples are selected, I want to look to check if there are any . (period) just for all , can someone explain to me what is wrong in my regex?

Thank you in advance


It's good:

 'object.all'.replace(new RegExp('(?!(\\.))all'), 'foo') // => object.foo 

For this, I expect the result to be "object.call":

 'object.call'.replace(new RegExp('(?!(\\.))all'), 'foo') // => object.cfoo 
+4
source share
2 answers

Although this seems complicated, it will do its job:

 function replaceAll(string, replacement) { var output = string.replace(/(\.)?all/g, function($0, $1) { return $1 ? $1 + replacement : $0; }); return output; } var test1 = replaceAll("object.all", "foo"); var test2 = replaceAll("object.call", "foo"); console.log(test1); // object.foo console.log(test2); // object.call 

I used the article ( Mimicking Lookbehind in JavaScript ) that @ m.buettner, linked in the comments earlier in a bit of code, I was working earlier this year.

0
source

The problem is that it still remains. Because of this, it is checked to see if a point exists in the same place as "a", and not in the place immediately.

0
source

Source: https://habr.com/ru/post/1436678/


All Articles