1

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)

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);
1
  • Tip, you can use \d as shorthand for [0-9] Commented Aug 17, 2014 at 9:10

2 Answers 2

1

There are no delimiters in RegExp constructor.

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

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you are missing any capturing groups. In RegEx these are groups between () If you rewrite it like this:

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

Then you an get the x and y with match1 and match[2]

Here is a demo

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.