Reputation:
I am searching a regex to find next word after "dog" for example, and delete it
"123 dog rabbit cat".replace(myregex, "");
"123 dog cat"
Thanks
edit: but
"123 dog <b> ok</b> cat".replace(myregex, "");
should not do anything
Upvotes: 0
Views: 502
Reputation: 214969
Expressions posted so far will fail on e.g. 123 dog <more spaces> rabbit cat
, so I think the \s+\S+
or \s+\w+
would be more accurate:
console.log("123 dog rabbit! cat".replace(/(dog)\s+\S+/, '$1')) // 123 dog cat
console.log("123 dog rabbit! cat".replace(/(dog)\s+\w+/, '$1')) // 123 dog! cat
I added !
to your string to show the difference between \S
and \w
.
Upvotes: 1
Reputation: 15042
You could use:
"123 dog rabbit cat".replace(/dog (.*?)( |$)/, "dog ");
Upvotes: 2
Reputation: 16971
Simplest way to do that:
"123 dog rabbit cat".replace(/(dog) \w+/, '$1')
Upvotes: 2