简体   繁体   English

Javascript正则表达式模式

[英]Javascript regular expression pattern

Can someone help me to figure what's wrong with my pattern? 有人可以帮我弄清楚我的图案有什么问题吗?

this is my text: sid='206' x='34.8395' y='32.1178'>×2 (206) 这是我的文字:sid ='206'x = '34 .8395'y = '32 .1178'>×2(206)

var regex = new RegExp("/x=\'[0-9]+\.[0-9]+\' y=\'[0-9]+\.[0-9]+\'/");

var match;
do {
    match = regex.exec(text);
    if (match) {
        console.log(match[1], match[2], match[3], match[4]);
    }
} while (match);

It looks like you are missing any capturing groups. 看来您缺少任何捕获组。 In RegEx these are groups between () If you rewrite it like this: 在RegEx中,这些是介于()之间的组,如果您这样重写它:

x=\'([0-9]+\.[0-9]+)\' y=\'([0-9]+\.[0-9]+)\'

Then you an get the x and y with match 1 and match[2] 然后,您将获得具有匹配项1和匹配项[2]的xy

Here is a demo 这是一个演示

There are no delimiters in RegExp constructor. RegExp构造函数中没有定界符。

You can use this regex: 您可以使用此正则表达式:

var re = /x='(\d+\.\d+)' +y='(\d+\.\d+)'/g; 
var str = "sid=\'206' x='34.8395' y='32.1178'>×2 (206)";

while ((m = re.exec(str)) != null) {
   console.log(match[1], match[2]);
}

RegEx Demo 正则演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM