html - Replace string in javascript -


i have string:

1<div>2</div><div>3</div><div>4</div> 

if replace values

var descriptionval = desc.replace('<div>', '-').replace('</div>', '-'); 

it replace first div

1-2</div><div>3</div><div>4</div> 

how replace div?

you have use regular expressions replace. can use replace function global flag g. checkout following solution:

var descriptionval = desc.replace(/<div>/g, '-').replace(/<\/div>/g, '-'); 

working example:

var str = "1<div>2</div><div>3</div><div>4</div>";  str = str.replace(/<div>/g, '-').replace(/<\/div>/g, '-');  document.write(str);

with simple string replace, example, first occurance of each replace function replaced:

var str = "1<div>2</div><div>3</div><div>4</div>";  str = str.replace('<div>', '-').replace('</div>', '-');  document.write(str);


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -