문제

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, '');

도움이 되었습니까?

해결책

You have to escape '|' character:

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top