Replace code with all characters except numbers and commas with JS _javascript tips

Source: Internet
Author: User

Use JS to replace all characters except numbers and commas

Copy Code code as follows:

<script language= "JavaScript" >
var str= "asdfk,asdf345345,345345";
Replaces 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>

Results:

Copy Code code as follows:

345345,345345


Complete code:

Copy Code code as follows:

Fckinsertdown.add = function (strtemp,str) {
if (Strtemp.indexof (",") >-1) {
Strtemp=dostr (strtemp);
var strs= new Array (); Define an array
Strs=strtemp.split (","); Character segmentation
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;
}

To repeat the 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 a) {
Data[data.length] = i;
}
return data;
}
For consumer calls
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 blank 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;
}


Cloud Habitat Community Original article, reproduced please indicate 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.