-2

I'm using Google maps to generate different locations off of a variable I'm passing in. However, I get an error from the API saying it cannot find the address and I think I know why. It's because I have a <br/> tag for the variable that is being outputted.

So for example when I alert the variable that's being passed through, it looks like this:

Deep Forest<br/>Suite 233<br/>100 Top Lane<br/>Los Angeles, CA

But I want the outputted variable to look like this.

Deep Forest Suite 233 100 Top Lane Los Angeles, CA

In short, I just want to replace all instances of <br/> with a empty space. I have all my different location variables laid out like this.

Here is what I tried:

var content = ("cookie.current.addressLabel"); // this outputs the current location address which is Deep Forest<br/>Suite 233<br/>100 Top Lane<br/>Los Angeles, CA 
         var text = $(content).text();
        //showGoogleMap(content);
        var regPat = /<br\s*[\/]?>/gi;
Curious13
  • 329
  • 2
  • 23
  • 2
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](http://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – André Dion May 08 '17 at 18:34
  • `text()` is going to strip out the HTML, if you want the `
    ` tags in there so you can replace them you need to use `.html()`
    – Rob M. May 08 '17 at 18:34
  • @AndréDion Although this may be a duplicate the marked duplicate is not right. This is an html() and regex question. Regular text matching wouldn't be cross browser. – Lime May 08 '17 at 18:53

2 Answers2

1

A simple replace should do the trick. No need for regex.

text.replace('<br/>', ' ');
Soviut
  • 88,194
  • 49
  • 192
  • 260
  • technically browsers render
    differently this isn't right as is plus he used .text instead .html
    – Lime May 08 '17 at 18:37
0

Try

var content = ("cookie.current.addressLabel"); // this outputs the current location address which is Deep Forest<br/>Suite 233<br/>100 Top Lane<br/>Los Angeles, CA 
         var html= $(content).html();
        //showGoogleMap(content);
        html.replace( /<br\s*[\/]?>/gi, ' ');
Lime
  • 13,400
  • 11
  • 56
  • 88