用于从 HTML 标记中提取值的 JavaScript RegEx
<font color="green">+4,13</font>%
我知道我不应该为此使用正则表达式,但这是我唯一的 html 的单一情况,所以......
我如何从上面的字符串中获取“4,13”?
编辑
上下文:
我正在通过 jQuery TableSorter 对表进行排序。一列包含 html 格式的数据,我无法更改它。我正在编写的自定义解析器有一个格式函数,我目前使用它来管理货币、百分比等...
现在,我想使用正则表达式检查收到的字符串是否是一个字符串。
format: function(s) {
console.log(s);
var stripped = s.replace("<font>","")
.replace("</font>", "");
return jQuery.tablesorter.formatFloat(stripped);
}
<font color="green">+4,13</font>%
I know that I shouldn't use regular expressions for that, but that's a single case where my only html is that so...
how can I get "4,13" from the string above?
EDIT
Context:
I am sorting a table via jQuery TableSorter. A column contains that html-formatted data and I can't change it. The custom parser I'm writing has a format function, which I currently use for managing currency, percentages and so on...
Now, I want to check, with a regex, if the string that comes to me is a string.
format: function(s) {
console.log(s);
var stripped = s.replace("<font>","")
.replace("</font>", "");
return jQuery.tablesorter.formatFloat(stripped);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该适用于您的具体示例 -
工作演示 - http://jsfiddle.net/ipr101/LHBp7/
This should work for your specific example -
Working demo - http://jsfiddle.net/ipr101/LHBp7/
编辑
如果您只想匹配数字
请参阅Regexr 上的此处
匹配可选的 +或 - 然后至少一位数字,然后是带有 . 的可选分数。或 a , 作为分数分隔符。
/Edit
尝试类似这样的操作
,然后您将在捕获组 1 中找到值“+4,13”。
请参阅 Regexr 上的此处
如果您想排除
+
而不是在捕获组之前添加它(可能是可选的)Edit
If you just want to match numbers
See it here on Regexr
Matches for an optional + or - then at least one digit, then an optional fraction with a . or a , as fraction delimiter.
/Edit
Try something like this
You will then find the value "+4,13" in the capturing group 1.
See it here on Regexr
If you want to exclude the
+
than add it (maybe optional) before the capturing group