从Javascript中的字符串中提取以特定字符开头的单词

Sat*_*hya 1 html javascript css

我有一个字符串,如下所示

var str = "This product price is £15.00 and old price is £19.00";
Run Code Online (Sandbox Code Playgroud)

我需要得到以"£"开头的词; 结果应为"£15.00""£19.00"我如何在Javascript中执行此操作?

Pra*_*lan 8

使用String#match方法

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));
Run Code Online (Sandbox Code Playgroud)