Use js to replace all characters except numbers and commas

Source: Internet
Author: User

Use js to replace all characters except numbers and commas

Copy codeThe Code is as follows:
<Script language = "javascript">
Var str = "asdfk, asdf345345, 345345 ";
// Replace all characters except numbers and commas.
Str = str. replace (/[^ 0-9,] */g ,"");
// Remove the first comma
If (str. substr (0, 1) = ',') str = str. substr (1 );
// Remove the second comma
Var reg =/, $/gi;
Str = str. replace (reg ,"");
Alert (str );
</Script>

Result:

Copy codeThe Code is as follows:
345345, 345345


Complete code:

Copy codeThe Code is as follows:
FCKinsertdown. Add = function (strtemp, str ){
If (strtemp. indexOf (",")>-1 ){
Strtemp = dostr (strtemp );
Var strs = new Array (); // defines an Array
Strs = strtemp. split (","); // delimiter
For (I = 0; I <strs. length; I ++ ){
If (I = strs. length ){
FCK. InsertHtml ("[downsoft]" + trim (strs [I]) + "[/downsoft]");
} Else {
FCK. InsertHtml ("[downsoft]" + trim (strs [I]) + "[/downsoft] <br/> ");
}
}
} Else {
FCK. InsertHtml ("[downsoft]" + dostr (strtemp) + "[/downsoft]");
}
}

Function dostr (str ){
Str = trim (str );
Var strarry = unique (str. split (","));
Str = strarry. join (",");
Str = str. replace (/,/ig ,",");
Str = str. replace (/[^ 0-9,] */ig ,"");
Str = str. replace (new RegExp (', +', "gm "),',');
If (str. substr (0, 1) = ',') str = str. substr (1 );
Var reg =/, $/gi;
Str = str. replace (reg ,"");
Return str;
}

// Deduplication Array
Function unique (data ){
Data = data | [];
Var a = {};
Len = data. length;
For (var I = 0; I <len; I ++ ){
Var v = data [I];
If (typeof (a [v]) = 'undefined '){
A [v] = 1;
}
};
Data. length = 0;
For (var I in ){
Data [data. length] = I;
}
Return data;
}
// For the user to call
Function trim (s ){
Return trimRight (trimLeft (s ));
}
// Remove the left blank
Function trimLeft (s ){
If (s = null ){
Return "";
}
Var whitespace = new String ("\ t \ n \ r ");
Var str = new String (s );
If (whitespace. indexOf (str. charAt (0 ))! =-1 ){
Var j = 0, I = str. length;
While (j <I & whitespace. indexOf (str. charAt (j ))! =-1 ){
J ++;
}
Str = str. substring (j, I );
}
Return str;
}
// Remove the white space on the right
Function trimRight (s ){
If (s = null) return "";
Var whitespace = new String ("\ t \ n \ r ");
Var str = new String (s );
If (whitespace. indexOf (str. charAt (str. length-1 ))! =-1 ){
Var I = str. length-1;
While (I> = 0 & whitespace. indexOf (str. charAt (I ))! =-1 ){
I --;
}
Str = str. substring (0, I + 1 );
}
Return str;
}

The original Article of the script home. For more information, see the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.