[英]Get decimal numbers using javascript regex
Sorry for the noob question. 对不起,菜鸟问题。
var regex = ??
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex);
How can I get the 1.00 using regex? 如何使用正则表达式获取1.00?
Thanks for the help.. 谢谢您的帮助..
Try this : 尝试这个 :
var regex = /id\="(\d+(\.\d+)?)"/i;
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex);
alert(number[1])
Forexample: 例如:
var regex = /[0-9\.]+/;
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex);
alert(number);
You can use: 您可以使用:
var a = '<span class="star" id="1.00"></span>';
var m = a.match( /\bid=(["']?)([^"']+)\1/i );
var id;
if (m)
id = m[2]; // 1.00
IMHO using regex wont solve your problem, you should first craete an html element from the string and then get the id. 恕我直言,使用正则表达式无法解决您的问题,您应该首先从字符串中创建一个html元素,然后获取ID。
var s = '<span class="star" id="1.00"></span>';
var div = document.createElement('div');
div.innerHTML = s;
var element = div.firstChild;
console.log(element.getAttribute("id"))
output: 输出:
1.00
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.