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
2 Answers
Reset to default 11searchString.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,"")