0

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 \

5
  • Can you provide some example data? Commented Mar 14, 2015 at 2:38
  • So, you want to catch all literal s that are not after a literal \ ? Commented Mar 14, 2015 at 2:39
  • @hwnd could you please show me how? Commented Mar 14, 2015 at 2:45
  • Are you replacing or matching? Commented Mar 14, 2015 at 2:47
  • I'm replacing s with ş but I don't want /s to be replaced Commented Mar 14, 2015 at 2:49

2 Answers 2

1

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 : 'ş'; });
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.