0

Sorry for the noob question.

var regex = ??
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex);

How can I get the 1.00 using regex?

Thanks for the help..

1
  • 2
    why dont you directly use the span tag and extract the id using javascript Commented Jun 12, 2014 at 6:56

4 Answers 4

1

Try this :

var regex = /id\="(\d+(\.\d+)?)"/i;
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex); 
 alert(number[1])
Sign up to request clarification or add additional context in comments.

Comments

1

Forexample:

var regex = /[0-9\.]+/;
var a = '<span class="star" id="1.00"></span>';
var number = a.match(regex);

alert(number);

3 Comments

this will match also <span class="star" bobo="1.00"></span>
This will return 5 if input text is <span class="star5" id="1.00"></span>
@Royi Namir: Sure, it was only regex example for this case. If I use your /id\="(\d+(\.\d+)?)"/i below, it takes eg. <div class=xy id="1.00">, etc. He has a string without bobo attribute, wioth no additional numbers.
0

You can use:

var a = '<span class="star" id="1.00"></span>';
var m = a.match( /\bid=(["']?)([^"']+)\1/i );
var id;
if (m) 
    id = m[2]; // 1.00

Comments

0

IMHO using regex wont solve your problem, you should first craete an html element from the string and then get the id.

var s = '<span class="star" id="1.00"></span>';
var div = document.createElement('div');
div.innerHTML = s;
var element = div.firstChild;

console.log(element.getAttribute("id"))

output:

1.00

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.