2

i Guys I need some help with some Regular expression:

here is the string:

<div style="width:477px;" id="__ss_8468630"><strong style="display:block;margin:12px 0 4px;"><a rel="nofollow" target="_blank" href="http://www.slideshare.net/Account/title" title="&#x002019;s DDM SaaS Antivirus Patch Management Solution">DDM SaaS Antivirus Patch Management Solution</a></strong> <div style="padding:5px 0 12px;"> View more documents from <a rel="nofollow" target="_blank" href="http://www.slideshare.net/account">Account</a> </div> </div>

and I need to get just the id number .

ex. output:

8468630

thanks everyone

4
  • will it always be in the same format? always the same number of digits? etc? Commented Oct 13, 2011 at 15:50
  • this one (id="_ss) is always the same Commented Oct 13, 2011 at 15:51
  • 1
    @MattBall - your sentiment is not entirely applicable in this case. He's trying to extract a portion of a string inside an HTML attribute. Yes he should use the parser to get the attribute value, but he still needs a regex or substring to get the part of the string he wants. Commented Oct 13, 2011 at 15:51
  • thanks Matt, good idea. My stupid head Commented Oct 13, 2011 at 15:59

5 Answers 5

3

No regEx necessary, you can just do a simple string replace.

element.id.replace('__ss_',''); // => '8468630'
Sign up to request clarification or add additional context in comments.

2 Comments

This will work, but assumes that the __ss_ part of the string will always be the same. He didn't specify this in the question, so a regex might have been better.
He says "here is the string", which seems like a pretty specific case. Even if the front-end of the string changes, as long as the length of the ID remains consistant you could still do it without a regex: element.id.substr(element.id.length-7, element.id.length); though that probably wouldn't be better than element.id.replace(/[^0-9]/g,'');
1
"__ss_8468630".match(/[\d\.]+/g); // --> [8468630]

Comments

0

This will get those digits. Although this will break if the # of digits changes.

\d{7}

Comments

0

Being e the div element you could use this

var n = e.id.match(/__ss_(\d+)/)[1];

As match returns an array containing the whole match in the position 0 (__ss_8468630) and the matching groups in its same position. Group 1 "(\d+)" (8468630)

Comments

0

You can do the following:

var num = element.id.replace(/\D*/g, '' )

Comments

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.