Sathya Sathya - 4 months ago 7
HTML Question

How to get a word that starts with a certain character from a string in Javascript

I'm having strong like this,

var str = "This product price is £15.00 and old price is £19.00";


I need to get the word that start with "£";
Result should be "£15.00" "£19.00"
How to do that in javascript?

Answer

Use String#match method

var str = "This product price is £15.00 and old price is £19.00";

// if `£` follows non-digit also then use
console.log(str.match(/£\S+/g));
// if `£` follows only number with fraction
console.log(str.match(/£(\d+(\.\d+)?)/g));