简体   繁体   中英

Javascript Regular Expressions problem

maybe I'm just misunderstanding Javascript's regular expression functionality but here goes... I have an array with expressions I want to remove, and I go about it this way:

var removeThese = ['inc\\.','inc','ltd\\.','ltd','\\(c\\)'];

for(var i=0; i < removeThese.length; i++) {
  var find = removeThese[i];
  regex = new RegExp('\\b'+find+'\\b','gi');
  titletext = titletext.replace(regex,'');
}

So, in the above I expect any island (full word) expressions of inc.,inc,ltd.,ltd or (c) to be matched. My console on console.log(regex):

/\binc.\b/gi
/\binc\b/gi
/\bltd\.\b/gi
/\bltd\b/gi
/\b\(c\)\b/gi

Looks pretty good right? But it's completely missing any occurances of (c) and when it replaces inc. it leaves the '.', so

This is a title (c) inc.

Becomes

This is a title (c) .

What am I missing here?

note, I would use a reg exp like '(inc\\.)|(inc)|(ltd\\.)...' but I have some items in that array that need special conversion (like 169 is converted to the © symbol before being searched for.

( and ) are not considered word characters, so there is no word boundary between whitespace and a ( . That means that your \\b won't match there.

You could change it to something like:

regex = new RegExp('(^|\\s+)'+find+'(?=\\s+|$)','gi');

Which will remove the word if it is either at the start of the string, or is preceded by some spaces, and at the end of the string, or followed by some spaces. It will also remove the spaces before the string so word (c) word2 will become word_word2 instead of word__word2 (Spaces marked by underscores for clarity).

You can also do this all with one regex. I find regular expressions a lot easier to declare inside the /match here/ syntax because you don't have to double escape things. Anyway, here's the entire thing in one line of code and one regex:

titletext = titletext.replace(/(^|\s+)(inc\.|inc|ltd\.|ltd|\(c\))(?=\s+|$)/gi,'');

You can see a little test app here: http://jsfiddle.net/jfriend00/wVDBr/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM