简体   繁体   中英

what's wrong with this javascript regexp?

Trying to find instances of "N/A" in markup and swap out with something else,

var $reg = new RegExp('[Nn]/[Aa]');

other variations were:

  • /[Nn]/[Aa]/
  • '^[Nn]/[Aa]'

I'd suggest:

stringToSearchIn.replace(/(N\/A)/gi,'words to replace with');
  1. \\/ escapes the slash, as the / character delimits the regex string, and
  2. the gi at the end:
    • g is for a global search, so it doesn't stop after the first match, and
    • i is case-insensitive (so it'll match n and N , a and A .

JS Fiddle demo

你可以使用这个:

N\/A

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