Quote:
How to implement 'and' operator in regex javascript
thechnically, it is a
or operator. As in Solution1, the RegEx will match the first word
or the second. Note that since
to is included in
too there are other possibilities to do the trick.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[
^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[
^]
Expresso Regular Expression Tool[
^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[
^]
[Update]
I am glad you appreciate this way of answering, I see it as the story of the fish.
Quote:
Give a fish to a man, he will eat for 1 day, teach him how to fish he will eat for its life.
[Update]
Be careful, Karthik Bangalore's answer work perfectly for the given example but would fail if there was an overlap on the words.
example:
if you look for replace
toto by
4, in this case, you end with
22.
var repWords = function (arg) {
return arg.replace(/too|to/gi, function myFunction(x) { return '2' }).replace(/toto/gi,function(y){return '4'});
}
In this case you need another solution.