1

I had a paragraph:

aaa-bbb-cc/my-text">my-text sas
//domain.com/my-text'> this is my-text

I want to replace all string 'my-text' to 'my replace tex' if they are not just after character '/' like:

aaa-bbb-cc/my-text">my replace text sas
//domain.com/my-text'> this is my replace text

Thank you

my notmypt
  • 55
  • 6

1 Answers1

2
(?<!\/)my-text

(?<!\/)Negative Lookbehind

since it's javascript, which does not support negative lookbehind, you can do this way:

(?=([^\/]|^)(my-text))

group 2 is your expect.

Shen Yudong
  • 1,190
  • 7
  • 14
  • with Javascript: https://stackoverflow.com/questions/641407/javascript-negative-lookbehind-equivalent – my notmypt Jan 19 '18 at 02:16
  • It doesn't work with me str = str.replace((/?=([^\/]|^)(my-text))/gi, "my replace text"); and this work: str = str.replace(/([\/])?my-text/gi, function($0,$1){ return $1?$0:'my replace text';}) – my notmypt Jan 19 '18 at 03:44
  • thanks @mynotmypt, (?=([^\/]|^)(my-text)) only can be used for match, can't do replace with it. – Shen Yudong Jan 19 '18 at 07:11