最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Removing ASCII « OR » from a string in JavaScript - Stack Overflow

programmeradmin2浏览0评论

I am trying to remove either « or » from a string to leave the remainding text in javaScript.

Have tried this to no effect:

$('body').delegate('.SearchPagination > a', 'click', function(e){
    e.preventDefault();
    var $this = $(this),
        txt = $this.text().toLowerCase(),
        txt = txt.replace(/«|»/g, ''),
        txt = $.trim(txt),
        page = '&page=';
        console.log(txt);
    if (txt === 'next' || txt === 'previous'){
        // get the current page value
        var active = $this.parent().find('.active').txt();
        if (txt === 'next'){
            // add one
            page += (active + 1);   
        } else if (txt === 'previous'){
            // subtract one
            page += (active - 1);
        }
    } else {
        // invoke search with the page number
        page += txt;
    }
    console.log(page);  
    //icisSite.icisSearch.search(page);
});

I am trying to remove either « or » from a string to leave the remainding text in javaScript.

Have tried this to no effect:

$('body').delegate('.SearchPagination > a', 'click', function(e){
    e.preventDefault();
    var $this = $(this),
        txt = $this.text().toLowerCase(),
        txt = txt.replace(/«|»/g, ''),
        txt = $.trim(txt),
        page = '&page=';
        console.log(txt);
    if (txt === 'next' || txt === 'previous'){
        // get the current page value
        var active = $this.parent().find('.active').txt();
        if (txt === 'next'){
            // add one
            page += (active + 1);   
        } else if (txt === 'previous'){
            // subtract one
            page += (active - 1);
        }
    } else {
        // invoke search with the page number
        page += txt;
    }
    console.log(page);  
    //icisSite.icisSearch.search(page);
});
Share Improve this question asked Jul 25, 2011 at 16:18 RyanP13RyanP13 7,78328 gold badges102 silver badges172 bronze badges 1
  • 1 This works fine: "«test»".replace(/«|»/g, ''). Your regexp is not wrong. – pimvdb Commented Jul 25, 2011 at 16:21
Add a ment  | 

2 Answers 2

Reset to default 4

Instead of using « in the regex, try copy-pasting the left arrow character displayed in the browser - i.e. the « sign. I tried that and it was replaced correctly.
So your regex should look like -

 txt = txt.replace(/«|»/g, '')

Have you tried:

txt = txt.replace('«','').replace('»', '');

But i don't think this is the point since your regexp is correct, so, just in case (you regulare expression seems correct to me) , are you shure that when you get the text the htmlentities are encoded in the first place?Maybe the page is in UTF-8 and the entities are not encoded but displayed withouth encoding

发布评论

评论列表(0)

  1. 暂无评论