1

There is a html comment with an Id that I need to extract. The comment is on a div, which is not hard to get using the JQuery $ operator. But the correct RegEx string I need I have not been able to figure out. This is the comment:

<!-- sid=FFKK12H1 -->

And I need a JS variable that has the string "FFKK12H1" assigned to. What is the correct syntax/expression to use? thanks!

EDIT:

I forgot a very important piece of information: The code needs to work on IE7. Unfortunately this is the browser my company allows us to use, and none of the proposed solutions work there so far. Any other thoughs?

1
  • For once a question in which parsing HTML with regexes is appropriate... Commented Feb 18, 2013 at 20:20

3 Answers 3

2

The regular expression would be: /<!-- sid=(.+?) -->/i:

var str = '<!-- sid=FFKK12H1 -->';
console.log(str.match(/<!-- sid=(.+?) -->/i)[1]);
Sign up to request clarification or add additional context in comments.

3 Comments

This could be made safer by making sure your group doesn't include any whitespace. Right now, if they add one more space after the code, your regex doesn't work properly.
this doesn't pass jslint.com...is this irrelevant technology now?
That'd be a separate requirement. If you want to make it pass jsLint, make it pass jsLint, which is simples.
0
var content = $('#comment-containg-div').html();
var regex = /<!--\s*sid=([\x00-\x7F]+)\s*-->/;
var matches = regex.exec(content);
console.log(matches);

The regex here is a amalgamted answer that includes all of the suggestions that other people on the page have made, it seems like it would be the safest to use.

Comments

0
var my_id = my_string.replace(/.*<!-- sid=(.*) -->.*/gi, '$1');

Example http://jsfiddle.net/YTdKQ/

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.