regex - Regular Expression - Match String Not Preceded by Another String (JavaScript) - Stack Overflow

I am trying to find a regular expression that will match a string when it's NOT preceded by anothe

I am trying to find a regular expression that will match a string when it's NOT preceded by another specific string (in my case, when it is NOT preceded by "http://"). This is in JavaScript, and I'm running on Chrome (not that it should matter).

The sample code is:

var str = ' www.stackoverflow';
alert(str.replace(new RegExp('SOMETHING','g'),'rocks'));

And I want to replace SOMETHING with a regular expression that means "match www.stackoverflow unless it's preceded by http://". The alert should then say " rocks", naturally.

Can anyone help? It feels like I tried everything found in previous answers, but nothing works. Thanks!

I am trying to find a regular expression that will match a string when it's NOT preceded by another specific string (in my case, when it is NOT preceded by "http://"). This is in JavaScript, and I'm running on Chrome (not that it should matter).

The sample code is:

var str = 'http://www.stackoverflow. www.stackoverflow.';
alert(str.replace(new RegExp('SOMETHING','g'),'rocks'));

And I want to replace SOMETHING with a regular expression that means "match www.stackoverflow. unless it's preceded by http://". The alert should then say "http://www.stackoverflow. rocks", naturally.

Can anyone help? It feels like I tried everything found in previous answers, but nothing works. Thanks!

Share Improve this question asked Sep 18, 2013 at 23:57 Assaf HershkoAssaf Hershko 1,9344 gold badges18 silver badges20 bronze badges 10
  • 1 Read about "negative lookaheads". – elclanrs Commented Sep 18, 2013 at 23:59
  • Techically, this whole string is not preceded by http://; should it be replaced as a whole? – raina77ow Commented Sep 19, 2013 at 0:03
  • Uncaught SyntaxError: Invalid regular expression: /(?<!http:\/\/)www\.stackoverflow\./: Invalid group – Assaf Hershko Commented Sep 19, 2013 at 0:03
  • 2 JS regex engine doesn't support lookbehinds. – raina77ow Commented Sep 19, 2013 at 0:05
  • 1 All I'm looking for is a RegEx that allows me to match string XXX if it's not preceded by YYY, in JavaScript. – Assaf Hershko Commented Sep 19, 2013 at 0:09
 |  Show 5 more ments

3 Answers 3

Reset to default 4

As JavaScript regex engines don't support 'lookbehind' assertions, it's not possible to do with plain regex. Still, there's a workaround, involving replace callback function:

var str = "As http://JavaScript regex engines don't support `lookbehind`, it's not possible to do with plain regex. Still, there's a workaround";

var adjusted = str.replace(/\S+/g, function(match) {
  return match.slice(0, 7) === 'http://'
    ? match
    : 'rocks'
});
console.log(adjusted);

You can actually create a generator for these functions:

var replaceIfNotPrecededBy = function(notPrecededBy, replacement) {
   return function(match) {
     return match.slice(0, notPrecededBy.length) === notPrecededBy
       ? match
       : replacement;
   }
};

... then use it in that replace instead:

var adjusted = str.replace(/\S+/g, replaceIfNotPrecededBy('http://', 'rocks'));

JS Fiddle.

raina77ow's answer reflected the situation in 2013, but it is now outdated, as the proposal for lookbehind assertions got accepted into the ECMAScript spec in 2018.

See docs for it on MDN:

Characters Meaning
(?<!y)x Negative lookbehind assertion: Matches "x" only if "x" is not preceded by "y". For example, /(?<!-)\d+/ matches a number only if it is not preceded by a minus sign. /(?<!-)\d+/.exec('3') matches "3". /(?<!-)\d+/.exec('-3') match is not found because the number is preceded by the minus sign.

Therefore, you can now express "match www.stackoverflow. unless it's preceded by http://" as /(?<!http:\/\/)www.stackoverflow./:

const str = 'http://www.stackoverflow. www.stackoverflow.';
console.log(str.replace(/(?<!http:\/\/)www.stackoverflow./g, 'rocks'));

This also works:

var variable = 'http://www.example. www.example.';
alert(variable.replace(new RegExp('([^(http:\/\/)|(https:\/\/)])(www.example.)','g'),'$1rocks'));

The alert says "http://www.example. rocks".

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356102a4624126.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信