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

regex - How to remove all special characters except numbers and space in a string using JavaScript? - Stack Overflow

programmeradmin5浏览0评论

I'm developing a Phonegap Android application. Now I want to pass some parameters from one page to another html page. I'm not using any server side methods. In the second page I want to get all passed parameters from first page using JavaScript.

For example here is an URL:

file:///C:/Users/dell/Projects/testapp1/search_result.html?searchstr=word1+*%26^+word2+3+word3+%40%23&city=city&showdishesnearby=false

The below function used to extract each parameters values:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
        function (m, key, value) {
            vars[key] = value;
        });
    return vars;
}

searchString = getUrlVars()["searchstr"]; // this is how I call above function to get value of passed particular parameter

Now I want to remove all special characters except numbers and white space in variable searchString.

I have used the below code:

searchString = searchString.replace(/[^a-zA-Z ]/g, " ");

but that doesn't solve my problem. How to remove all special characters except numbers and space in a string using JavaScript?

I'm developing a Phonegap Android application. Now I want to pass some parameters from one page to another html page. I'm not using any server side methods. In the second page I want to get all passed parameters from first page using JavaScript.

For example here is an URL:

file:///C:/Users/dell/Projects/testapp1/search_result.html?searchstr=word1+*%26^+word2+3+word3+%40%23&city=city&showdishesnearby=false

The below function used to extract each parameters values:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
        function (m, key, value) {
            vars[key] = value;
        });
    return vars;
}

searchString = getUrlVars()["searchstr"]; // this is how I call above function to get value of passed particular parameter

Now I want to remove all special characters except numbers and white space in variable searchString.

I have used the below code:

searchString = searchString.replace(/[^a-zA-Z ]/g, " ");

but that doesn't solve my problem. How to remove all special characters except numbers and space in a string using JavaScript?

Share Improve this question edited Nov 12, 2013 at 7:45 Qantas 94 Heavy 16k31 gold badges72 silver badges88 bronze badges asked Nov 12, 2013 at 5:47 Chandrakanth MChandrakanth M 3011 gold badge4 silver badges11 bronze badges 2
  • 3 str.replace(/[^\d\s]+/, '').replace(/\s{2,}/, ' ') – falsetru Commented Nov 12, 2013 at 5:48
  • Sorry Mr.falsetru your solution also didn't work for me. Please check the question, i have given more explanation about the problem. – Chandrakanth M Commented Nov 12, 2013 at 7:41
Add a ment  | 

2 Answers 2

Reset to default 11
searchString.replace(/[^a-z\d\s]+/gi, "");

removes all but letters, numbers and whitespace.

var s = 'keep%8$this part 3£$@plz £$% @£';
s.replace(/[^a-z\d\s]+/gi, "");
// "keep8this part 3plz  "

Simply add below code to replace all alpha-numeric character(punctuation, pace, underscore)

var string = str.replace(/[^A-Za-z0-9]/g,"")

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论