Click here to Skip to main content
14,980,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If a string has words like 'too' and 'to', replace it with the number 2. While I was able to implement this task using only one of the words.
repWords('today is a good day too') -> '2day is a good day 2'

What I have tried:

 var repWords = function (arg) {
 return arg.replace(/to/g, "2")
  }
console.log(repWords("today is a wonderful day too"))

How do I make it work with both words?
Posted
Updated 7-Aug-16 5:21am

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.
JavaScript
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.
   
v3
Comments
AlexLearne 7-Aug-16 11:25am
   
You came to the rescue again. I really like the fact you are giving me directions but not the actual answers. This makes me think.Thank you)
try this

JavaScript
var repWords = function (arg) {
            return arg.replace(/too|to/gi, function myFunction(x) { return '2' });
        }
        console.log(repWords("today is a wonderful day too"))


refer :JavaScript String replace() Method[^]

Fiddle: link[^]
   
v3
Comments
[no name] 7-Aug-16 10:48am
   
I like your fiddle examples, a 5.
Karthik_Mahalingam 7-Aug-16 10:49am
   
Thank you :)
AlexLearne 7-Aug-16 10:58am
   
Thank you very much, I know it is not on the task but I have never seen the example you gave me. How would you go about solving this, let`s assume we also want to add a word 'for' which would return 4 instead, would I need to use the same approach? Again, thank you!
Karthik_Mahalingam 7-Aug-16 11:14am
   
yes you can.

var repWords = function (arg) {
return arg.replace(/too|to/gi, function myFunction(x) { return '2' }).replace(/for/gi,function(y){return '4'});
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900