admin管理员组

文章数量:1414947

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 e 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).

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 e 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).

Share Improve this question edited Oct 13, 2011 at 17:49 James Hill 61.9k22 gold badges149 silver badges166 bronze badges asked Oct 13, 2011 at 17:26 duncanduncan 31.9k15 gold badges81 silver badges101 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 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');

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.

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]

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/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?

本文标签: jqueryjavascript regexp replacemultilineStack Overflow