views:

89

answers:

2

I have a string in my code that I receive that contains some html tags. It is not part of the HTML page being displayed so I cannot grab the html tag contents using the DOM (i.e. document.getElementById('tag id').firstChild.data);

So, for example within the string of text would appear a tag like this:

 <span id='myQty'>12</span>

My question is how would I use a regular expression to access the '12' numeric digit in this example? This quantity could be any number of digits (i.e. it is not always a double digit).

I have tried some regular expressions, but always end up getting the full span tag returned along with the contents. I only want the '12' in the example above, not the surrounding <span> tag. The id of the <span> tags will always be 'myQty' in the string of text I receive.

Thanks in advance for any help!

A: 
var testfunction = function (input) {
    var regvar = new RegExp(/^(<span id\='myQty'>\w+<\/span>)$/);
    if (regvar.test(input) === true) {
        input = input.slice(6, input.length - 7);
    }
    return input;
}
A: 

Since you're just trying to get a specific value from inside a specific tag structure, and not trying to use regular expressions to strip HTML tags:

var myQtyMatch = str.match(/<span id='myQty'>(\d+)<\/span>/);
if (myQtyMatch) {
    var myQty = myQtyMatch[1];
    // myQty now holds the value between the <span> tags.
}
Jon Benedicto
Thanks everyone for the quick help... I actually implemented Jon's logic. Worked great!
Nathan Hernandez