下面將爲您提供前/後$所有數字:
var data = '$22.50 + taxes ' +
'4 Premium Plan Gifts for $150.00 + taxes ' +
'3 Forfait supérieur cadea... de 150,00 $ taxes en sus';
console.log(data.match(/(\$[0-9,.]+|[0-9,.]+\s*\$)/g)); // ["$22.50", "$150.00", "150,00 $"]
擊穿:
/ # regex start
(# capturing group start
\$ # literal $
[0-9,.]+ # 1 or more of the following characters "0", "1", .., "9", ",", "."
| # or operator (meaning ether left hand side or right hand side needs to be true
[0-9,.]+ # 1 or more of the following characters "0", "1", .., "9", ",", "."
\s* # 0 or more spaces
\$ # literal $
) # capturing group end
/# regex end
g
如果你想刪除的文字$您可以使用此:
console.log(data.replace(/(?:\$([0-9,.]+)|([0-9,.])+\s*\$)/g, "$1$2")); // 22.50 + taxes 4 Premium Plan Gifts for 150.00 + taxes 3 Forfait supérieur cadea... de 0 taxes en sus
注意事項:
(?:) # non capturing group (will not produce $1, $2, ...)
[像這樣的東西(http://jsfiddle.net/hpykuy59/1/)? –