1

I am trying to parse a webpage and to get the number reference after <li>YM#. For example I need to get 1234-234234 in a variable from the HTML that contains

<li>YM# 1234-234234         </li>

Many thanks for your help someone!

Rich

1
  • var text = document.body.innerHTML; console.log(text); text = text.replace(/(<([^>]+)>)/ig,''); var id = text.match(/YM#[0-9]-[0-9]/g); I know I am a way off! Commented Apr 21, 2012 at 5:57

3 Answers 3

1

currently, your regex only matches if there is a single number before the dash and a single number after it. This will let you get one or more numbers in each place instead:

/YM#[0-9]+-[0-9]+/g

Then, you also need to capture it, so we use a cgroup to captue it:

/YM#([0-9]+-[0-9]+)/g

Then we need to refer to the capture group again, so we use the following code instead of the String.match

var regex = /YM#([0-9]+-[0-9]+)/g;
var match = regex.exec(text);
var id = match[1];
 // 0: match of entire regex
 // after that, each of the groups gets a number
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for telling me exactly how to do it. It didn't actually work initially but it was so close I managed to fix it myself. It needed \s in between # and [ as there is a space. Thanks so much Jasper!
1

(?!<li>YM#\s)([\d-]+)

http://regexr.com?30ng5

This will match the numbers.

Comments

1

Try this:
(<li>[^#<>]*?# *)([\d\-]+)\b
and get the result in $2.

1 Comment

Try: result = subject.replace(/(<li>[^#<>]*?# *)([\d]+)\b/g, "$2");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.