Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Regex select a word between two words

Tags:

javascript

Helllo, ajax request returs me a string, that I store in a variable:

text = "bla bla bla word1 unknown. Word2 bla bla bla";

I know the every word in the text except the 'unknown'. I need to store the 'unknown' word in a variable to do further work. I know that it can be done by Regex, but I don't quite understand it. Can someone show me the trick? Thank you for your time!

like image 777
user2840278 Avatar asked Nov 27 '25 16:11

user2840278


1 Answers

You can use a simple .match like this.

text = "bla bla bla word1 word i want Word2 bla bla bla";
myWord = text.match("word1(.*)Word2")[1];
console.log(myWord);
like image 141
Amit Avatar answered Nov 30 '25 05:11

Amit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!