我正在使用这个正则表达式:
a = new window.RegExp('(£...)');
到XMLHttprequest的响应文本:
d= a.exec(Xml.responseText);
其中'Xml.responseText'是包含以下页面的html源代码的字符串:
http://www.amazon.co.uk/gp/aw/d/B0050IIVNK
到目前为止,我已经能够匹配此页面中的任何表达式,并且完全没有问题使用相同的代码。但是当匹配角色'£'时,'d'变为'null'(并且它不应该因为页面中至少有两个theese字符。
有人知道为什么会这样吗?
感谢。
编辑:按照MelanciaUK的建议搜索编码值\ xa3解决了问题,谢谢。
答案 0 :(得分:0)
问题不在于您的正则表达式,您可以使用静态字符串自行检查:
a = new window.RegExp('£...');
d= a.exec("This ring costs £205.50 but I only can spend £150 today.");
// ["£205"]
但我会用更好的方法来检索金额:
/*
(
£ # £ symbol
\d+ # followed by 1+ number
(?: # non-captured group
\. # a dot
\d+ # followed by 1+ number, for decimals
)? # decimals are optional
)
*/
var regex = /(£\d+(?:\.\d+)?)/g;
var input = "This ring costs £205.50 but I only can spend £150 today.";
console.log(input.match(regex)); //["£205.50", "£150"]
编辑:根据Bergi建议更改了正则表达式文字的可读性