Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34152

regular expression disallow in javascript

I want to select all literal letter s but not literal word \s

(?<!\\)s

works in c# but I'm not able to adjust it to work with javascript. how do I disallow literal \s in javascript matching all literal s?

for example int the expression: test\ss should match test\ss

Edit: as Mitch says I want to catch all literal s that are not after a literal \

Upvotes: 0

Views: 28

Answers (2)

hwnd
hwnd

Reputation: 70722

You can create DIY Boundaries ...

var r = 'test\\ss'.replace(/(^|[^\\])s/gi, '$1ş');
console.log(r); //=> 'teşt\sş'

Or use a workaround:

var r = 'test\\ss'.replace(/(\\)?s/gi, function($0,$1) { return $1 ? $0 : 'ş'; });

Upvotes: 1

Dimitris Karagiannis
Dimitris Karagiannis

Reputation: 9358

According to your comment in your question, try this then

/(?:\B|\s)s/g

Try this in your browsers console to confirm it works

re = /(?:\B|\s)s/g;
str = 'test\\ss';
res = str.match(re)
console.log(str.replace(re, '0'));

res will have 2 results in it

Upvotes: 0

Related Questions