Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  46] [ 6]  / answers: 1 / hits: 8024  / 11 Years ago, tue, december 17, 2013, 12:00:00

How to get a word after specific word?



For example: I have string like this :



string = Price:$20 is very cheap 


I want get result: $20



How can I do that with Javascript?


More From » html

 Answers
3

You can use regex:



var matches = /Price:(.*?) /g.exec(Price:$20 is very cheap);
if(matches.length > 1) {
var price = matches[1];
}
else {
// Not found
}


Note this will match any sequence of characters after the word Price:. If there is whitespace after the word price, it will break. If there is no space after $20, it will break. You need to make the rules more complex depending on your input source.


[#49464] Monday, December 16, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucillemariselal

Total Points: 108
Total Questions: 97
Total Answers: 119

Location: Thailand
Member since Thu, May 6, 2021
3 Years ago
;