Replace special characters with alphabet in jQuery

时间:2016-12-02 04:46:56

标签: php jquery regex

How to remove Special Character and alphabet from variable by jQuery.

I have

var test = +985

But I want to get value as

var another = 985

It should not allow character as well + as I am using it in Phone Code

3 个答案:

答案 0 :(得分:2)

try to something like this...

var inputString = "~!@#$%^&*()_+=`{}[]|\:;'<>,./?Some actual text to keep, maybe...",
var outputString = inputString.replace(/([~!@#$%^&*()_+=`{}\[\]\|\\:;'<>,.\/? ])+/g, '-').replace(/^(-)+|(-)+$/g,'');

答案 1 :(得分:1)

Might be duplicate of this

You just need to use replace function it's first parameter is symbol you want to replace and another is symbol or word you want to replace with.

For more info please visit Javascript replace function

var test = "+985";
var another = test.replace("+", "");

答案 2 :(得分:1)

Try this to extract number:

var test = +985;
alert(test+"".match(/\d+/));