Regex: Replace foo if it is a word or inside a URL

Considering this:

str = "foo myfoo http://thefoobar.com/food is awesome"; str.replace(magicalRegex, 'bar'); 

Expected Result:

 "bar myfoo http://thebarbar.com/bard is awesome" 

I get the \b(foo)\b , but I can't figure out how to combine and catch foo from the url. For these purposes, suppose URLs always begin with http .

Any help?

+4
source share
3 answers

You can use this code (works well with your example, but have not tried with more complex inputs):

 str = 'foo myfoo http://thefoobar.com/food is awesome'; str = str.replace(/\bfoo\b/g, 'bar'); while (/http:\/\/[^\s]*?foo/.test(str)) str = str.replace(/(http:\/\/[^\s]*?)?foo/g, function($0, $1) { return $1 ? $1 + 'bar' : $0; }); console.log(str); 

OUTPUT:

 bar myfoo http://thebarbar.com/bard is awesome 

Live Demo: http://ideone.com/8xGy2h

+4
source

I think you need to take many steps so that everything is done correctly. You basically do two separate (albeit similar) regular expressions:

  • globally replacing the group of characters "foo" if it occurs inside a link, and
  • global word replacement "foo" in the rest of the line.

This code will go through both stages separately (first the URL, the other lines the second) and give the final replacement:

 var urlPattern = /(http:\/\/[^\s]+)/; var urlFooPattern = /(foo)/g; var globalFooPattern = /\b(foo)\b/g; var str = "foo myfoo http://thefoobar.com/food is awesome"; var urlString = str.match(urlPattern)[0]; urlString = urlString.replace(urlFooPattern, "bar"); str = str.replace(urlPattern, urlString); str = str.replace(globalFooPattern, "bar"); 

Note. . It is assumed that there is only one URL per line., To handle the possibility of multiple URLs would be a little more complicated:

  • grab all urls using var urlString = str.match(urlPattern) in an array
  • creating a new array by cycling through each URL and individually "replacing foo" on each
  • Quoting through the original array of matches and using them as templates to replace with updated values ​​in the second array

scrolling through all the URLs returned by var urlString = str.match(urlPattern) , replacing them with β€œfoo” individually and repeating them again, then replacing them in the original line one at a time.

0
source

If you want to use borders only to match β€œfoo” and not β€œmyfoo”, you will need to use the operation or ( | ) to match the URLs - if necessary, if β€œfoo” is turned on in the middle of the URL, it won’t surrounded by word boundaries.

Something like this should work for you:

 \b(foo)\b | http\S*(foo)\S* 

You can continue further tests here if necessary.


EDIT: Sorry, I thought the OP wants to capture these words and URLs. As far as I know, Look-behind regular expressions that won't fix the root of the replacement URL are not supported in JS, but can often be duplicated using a simple function, see here to discuss how to do this .

-1
source

Source: https://habr.com/ru/post/1468811/


All Articles