0

I have some texts returned by Google Maps, I would like to remove the div tags, (replacing them by <br /> would be the perfect deal

'Turn <b>right</b> onto <b>Route de Sospel/D2566</b>
     <div style="font-size:0.9em">Continue to follow D2566</div>
    <div style="font-size:0.9em">Go through 6 roundabouts</div>'

=>

'Turn <b>right</b> onto <b>Route de Sospel/D2566</b><br />
Continue to follow D2566<br />Go through 6 roundabouts'

I don't know which solution would be better either find the appropriate regex, might be hard, but more performant than evaluating this string with $() Jquery and them find('div').text() inside ...

thanks for your advices (or sample code)

  • 5
    TONY THE PONY HE COMES! http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – Darkzaelus Feb 29 '12 at 11:55
  • 2
    jQuery is just fine for this. Using RegEx will almost always end with tears. – Shadow The GPT Wizard Feb 29 '12 at 11:55
  • Careful with your FUD there - did you know that jQuery itself uses RegEx for a number of things ? – Russ Clarke Feb 29 '12 at 12:00
  • @RussC true, yep in minimal cases RegEx is sensible, but people do love memes! ;) – gideon Feb 29 '12 at 12:01
  • quite :) It's just that RegEx is powerful and useful (I know, great power great responsibility etc) and people are scared of using it, often going to great lengths to reinvent the wheel and mostly just implementing a bad way of doing what regex would do anyway, but without the years of testing and process that goes into a defined sys....zalgo he comes – Russ Clarke Feb 29 '12 at 12:04

2 Answers2

3

Assume your content was like this:

<div id="content">
Turn <b>right</b> onto <b>Route de Sospel/D2566</b>
     <div style="font-size:0.9em">Continue to follow D2566</div>
    <div style="font-size:0.9em">Go through 6 roundabouts</div>
<div>​

This should work, just replace the #content selector according to how you get your data:

​$("#content​​​​​​​​​ div").each(//get all div's inside #content
    function(i,el) {//i is an index el refers to each element.
        $(el).replaceWith("<br/>" + $(el).text());
    }//replace the element with the text of each element
);​​​​​​​​
//alert($("#content").html());

​See working demo: http://jsfiddle.net/rYeUH/

gideon
  • 19,329
  • 11
  • 72
  • 113
0

You can use ordinary string Replace

var googleMapsHtml = "{your data}";
googleMapsHtml = googleMapsHtml.replace('<div style="font-size:0.9em">', "");
googleMapsHtml = googleMapsHtml.replace('</div>', '<br />');

Be aware that html provided by Google Maps might change in the future and your string manipulation will fail easily.

Tx3
  • 6,796
  • 4
  • 37
  • 52
  • yes why not, If you find a regex to match
    would be better
    –  Feb 29 '12 at 12:52
  • 1
    I think any kind of string modification of some received html is a bad idea. Maybe loading it to some sort of HtmlDocument (C#, Java etc.) in the server side and then modifying elements.. – Tx3 Feb 29 '12 at 12:55