Question

My question may find simple, But I am not getting it right. How could I remove "|" from a string so far I have used the following but Its not working

<div class="inner"> | ABCD || </div>
var txt=divVar.html();

1) txt=txt.remove("|");

2)txt=txt.replace (/|/g, '');

Was it helpful?

Solution

You have to escape '|' character:

txt=txt.replace (/\|/g, '');

OTHER TIPS

You can use split and join:

var newTxt = txt.split('|').join('');

To remove white space, you can use $.trim():

var newTxt = $.trim(txt.split('|').join(''));

Demo

If you want to remove just '|' and not '||' than you can use the below mentioned solution

<div class="inner"> | ABCD || </div>
 <script type="text/javascript">
    $(document).ready(function () {
        var str = $(".inner").html();
        alert(str);
        var FormatedStr = str.trim().replace('|', '');
        alert(FormatedStr);
    });
</script>

Else, if you want to replace both than use below

<div class="inner"> | ABCD || </div>

   <script type="text/javascript">
        $(document).ready(function () {
            var str = $(".inner").html();
            alert(str);
            var FormatedStr = str.trim().replace('|', '').replace('||', '');
            alert(FormatedStr);
        });
    </script>

Hope this help you to solve your problem

Thanks Prashant

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top