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
Post a Comment