Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp replace any single word

I need to replace 'page.a = b' with 'page.a = node.b'. In a word, i need to turn single word 'b' into 'node.b'; In other word, the string 'page.a = b && page.a < c' shold be transformed into 'page.a = node.b && page.a < node.c', just add 'node.' as the prefix to the single words. Can anyone help me with this? I think the js regexp can work but just cannot find the way with it.

let origin = 'page.a = b'
let expected = 'page.a = node.b';

How do i get the expected string from the origin ?

like image 337
Lou Kai Avatar asked Mar 25 '26 18:03

Lou Kai


1 Answers

You can use negative lookarounds to look for words that aren't preceded by or followed by a dot or a word character:

(?<![.\w])(\w+)(?![.\w])

and replace the matching words with:

node.$1

https://regex101.com/r/GD2N9g/5

like image 164
blhsing Avatar answered Mar 27 '26 09:03

blhsing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!