javascript - Get list of words containing substring in string -


i have script runs if value entered contains substring 'sn' need ignore instances of substring if followed either letter t or apostrophe.

the reason value entered contains serial number of device , script can pick serial number out because preceded letters 'sn'. problem if user enters words such "wasn't" or "isn't" (can or without apostrophe depending on entered it) script gets last instance of 'sn'. need ignore words.

the code use check 'sn' is...

var lowercase_name = subject.tolowercase(); var has_sn = lowercase_name.indexof("sn") > -1; if(has_sn === true){     //do } 

to string, indexof not enough since cannot have exception. enough though regexp. condition "ignore instances of substring if immediately". mean should use negative ahead ((?!)).

in case, regexp shoud :

var lowercase_name = subject.tolowercase(); var has_sn = lowercase_name.match(/sn(?!['t])/); if(has_sn){     //do } 
  • (?!) = negative ahead. search sn not followed what's inside after !
  • [] = "or" character. match of character inside brackets.

now, if want make more complex task, should take @ regexp tutorials


Comments