JavaScript Replace only replacing the first

I am using the following code to allow line breaks in the JQuery UI tooltip function.

$(function() {
    $( document ).tooltip();

    $('.linebreak').tooltip({ 
        content: function(callback) { 
            callback($(this).prop('title').replace('|', '<br />')); 
        }
    });
});

The problem is that it only replaces the | to <br /> on the first occurrence and then just leaves the | in place for the rest.

Is there a way to replace all of them, not just the first?

Answer:1

To perform a global replacement you can use g:

Try this

callback($(this).prop('title').replace(/\|/g, '<br />'));

More infor is HERE.

Answer:2
replace(/\|/g, '<br />')

Have a look at this previous SO answer: What does the regular expression /_/g mean?

See this fiddle: http://jsfiddle.net/U3VxQ/

Answer:3

In my Angular app the following sequence of events needs to happen: User clicks a button A previously hidden div is shown by flipping a boolean scope variable to which an ng-show is bound The newly ...

In my Angular app the following sequence of events needs to happen: User clicks a button A previously hidden div is shown by flipping a boolean scope variable to which an ng-show is bound The newly ...

Often I have maps with a whole lot of markers (let's say representing shops). I have infowindows that display basic info on the shop represented by the marker, then, on the infoWindow HTML I like to ...

Often I have maps with a whole lot of markers (let's say representing shops). I have infowindows that display basic info on the shop represented by the marker, then, on the infoWindow HTML I like to ...

I want to create a html5 audioplayer for my band's website. The audiofiles are stored in the database as mp3, ogg and wav files to support all major browsers on their current versions. First, all ...

I want to create a html5 audioplayer for my band's website. The audiofiles are stored in the database as mp3, ogg and wav files to support all major browsers on their current versions. First, all ...

I have two static pages from a user which is deployed on their servers. Currently I am making a call to their server from code behind and loading them. But now I don't want to make a code behind call. ...

I have two static pages from a user which is deployed on their servers. Currently I am making a call to their server from code behind and loading them. But now I don't want to make a code behind call. ...

  1. load website inside iframe
  2. load webpage into iframe
  3. html load webpage in iframe
  4. load external webpage in iframe