What do I need to apply
Find patterns in a string, match matches Change
For example, I have an array of patterns
patters = [/ MRS.S.S. / MRSSSA]]; StringSubject = "Mr. John is an expert; Mr. John is not short; Mr. Watson knows this very well; After removing matches, it may look like a case insensitive (case). stringSubject = "{1} John is an expert. {2} John is not less {3} Watson knows this very well ";
and token array
token = [" Mr. "," Mr. "," MRS "]] stringSubject =" {1} John is an expert. {3} John is not short {2} Watson knows this very well ";
// After processing the string
The tokens are changed such that
< Pre> stringSubject = "Mr. John is an expert. John is not very short, Mr Watson knows very well ";
so that the original string can be retrieved, even after doing case insensitive operations for matching patterns.
How can this be done with regex?
How is it?
var stringSubject = "Mr. John is ...", Pattern = [/ MRS //// MRS.S.S.], tritukinandx = 0, token = [/ * This is where the matches are stored (Var i = -1, l = pattern.length; ++ i & lt; l;) {stringSubject = stringSubject.replace (pattern [i], function (match) {token [currentTokenIndex] = Match; return '{' + current tokenindex ++ + '}';});} StringSubject; // "l =" {0} is John ... "// later: stringSubject = stringSubject.replace (/ \ {(\ D +?) \} / G, function (match, index) {return tokens [index];}); stringSubject; // & lt; = "Mr. John is ..."
< / Pre>
Comments
Post a Comment