2

I have the following string :

var str='
                <span class="productName">Basa fillets</span><br>
                Brand: 
                <span class="brandName">COMPLIMENTS</span><br>
                400 <abbr title="Gram" lang="en">gr</abbr>
            '

I need to get the '400' (could be a word,or even a sentence). What I have so far is :

d = str.replace(/<br>/g,'').replace(/<.*<\/.*>/g,'').replace(/\n/g,'').replace(/ */g,'').replace(/brand:/i,'');

It works but... well, I'm sure I can do better. i have plenty of similar queued replace in my code, and I'd like to know how to improve that so i'm more looking for a general answer than a particular solution.

Thanks!

2
  • 1
    Your string isn't valid you either need to escape the " symbol with \" or use ' inside the main string. changing it to var str="<span class='productName'>Basa fillets</span><br>Brand: <span class='brandName'>COMPLIMENTS</span><br>400 <abbr title='Gram' lang='en'>gr</abbr>"; Commented Oct 29, 2013 at 15:11
  • 1
    or change the "" around it with '. Edited, was obviously a typo... Commented Oct 29, 2013 at 15:16

3 Answers 3

5

Instead of using string tools/regex on this, you can use DOM methods on it (it is HTML).

First you make a "fake" div and add the HTML to it.

var str="\
                <span class=\"productName\">Basa fillets</span><br>\
                Brand: \
                <span class=\"brandName\">COMPLIMENTS</span><br>\
                400 <abbr title=\"Gram\" lang=\"en\">gr</abbr>\
            ";

var fakeDiv = document.createElement('div');
fakeDiv.innerHTML = str;

Then just use normal DOM traversal methods to get the node you need. There are many ways to get to the element, depending on your HTML.

var brandName = fakeDiv.getElementsByClassName('brandName');

var textNode = brandName[0].nextSibling.nextSibling;

console.log(textNode.nodeValue.trim());

DEMO: http://jsfiddle.net/aqpgV/

Or, you can start from the <abbr> element and work backwards.

var gram = fakeDiv.getElementsByTagName('abbr');

var textNode = gram[0].previousSibling;

console.log(textNode.nodeValue.trim());

DEMO: http://jsfiddle.net/aqpgV/1/

However you traverse it is up to you :-)

Sign up to request clarification or add additional context in comments.

4 Comments

Great answer, I wouldn't have thought of the fakeDiv! thanks a lot!
Glad I could help! :-D Yeah, you don't actually need elements to actually be in the DOM to be able to use them ;-)
If you need to support IE8, you won't have getElementsByClassName, but you could use fakeDiv.querySelector('.brandName'); since there is only one element with class "brandName".
@TomPietrosanti: Thanks. I forgot getElementsByClassName doesn't work in old browsers.
0

Regex

class="brandName">[^<]+</span><br>[^\w]+([^<]+) <abbr title=

Regular expression visualization

Debuggex Demo

Notes: Group 1 will contain the item you want.

2 Comments

sexy! but my teammates will pull their hair with that one :-D thanks!
@xShirase completely understandable, I honestly think using html's native concepts and manipulation for getting items is more appropriate for this. Just wanted to give you a quick example of some RegEx.
0

If you wanted to use regex, you could do something like this.

    var str="\
            <span class=\"productName\">Basa fillets</span><br>\
            Brand: \
            <span class=\"brandName\">COMPLIMENTS</span><br>\
            400 <abbr title=\"Gram\" lang=\"en\">gr</abbr>\
        ";

    var myRegexp = /COMPLIMENTS<\/span><br>\W(.*?) <abbr /g;
    var match = myRegexp.exec(str);
    alert(match[1]);

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.