3

I have some text content (read in from the HTML using jQuery) that looks like either of these examples:

<span>39.98</span><br />USD

or across multiple lines with an additional price, like:

<del>47.14</del>

    <span>39.98</span><br />USD

The numbers could be formatted like

  • 1,234.99
  • 1239,99
  • 1 239,99

etc (i.e. not just a normal decimal number). What I want to do is get just whatever value is inside the <span></span>.

This is what I've come up with so far, but I'm having problems with the multiline approach, and also the fact that there's potentially two numbers and I want to ignore the first one. I've tried variations of using ^ and $, and the "m" multiline modifier, but no luck.

var strRegex = new RegExp(".*<span>(.*?)</span>.*", "g");

var strPrice = strContent.replace(strRegex, '$1');

I could use jQuery here if there's a way to target the span tag inside a string (i.e. it's not the DOM we're dealing with at this point).

4 Answers 4

2

You could remove all line breaks from the string first and then run your regex:

strContent = strContent.replace(/(\r\n|\n|\r)/gm,"");
var strRegex = new RegExp(".*<span>(.*?)</span>.*", "g");
var strPrice = strContent.replace(strRegex, '$1');
Sign up to request clarification or add additional context in comments.

Comments

2

This is pretty easy with jQuery. Simply wrap your HTML string inside a div and use jQuery as usual:

var myHTML = "<span>Span 1 HTML</span><span>Span 2 HTML</span><br />USD";
var $myHTML = $("<div>" + myHTML + "</div>");

$myHTML.find("span").each(function() {
   alert($(this).html()); 
});

Here's a working fiddle.

Comments

1

try using

"[\s\S]*<span>(.*?)</span>[\s\S]*"

instead of

".*<span>(.*?)</span>.*"

EDIT: since you're using a string to define your regex don't forget to esacpe your backslashes, so

[\s\S] 

would be

[\\s\\S]

3 Comments

Good answers from all respondents, but yours was the closest to what I was trying to do, and it works nicely, thanks!
Why does the second character set [.\s\S] contain the . ? It also seems to work with just [\s\S]
@duncan, that was a typo. fixing now.
1

You want this?

var str = "<span>39.98</span><br />USD\n<del>47.14</del>\n\n<span>40.00</span><br />USD";

var regex = /<span>([^<]*?)<\/span>/g;

var matches = str.match(regex);

for (var i = 0; i < matches.length; i++)
{
    document.write(matches[i]);
    document.write("<br>");
}

Test here: http://jsfiddle.net/9LQGK/

The matches array will contain the matches. But it isn't really clear what you want. What does there's potentially two numbers and I want to ignore the first one means?

1 Comment

I want the number in the <span></span>, but I don't want the number in the <del></del>

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.