problem regarding the white spaces and trimming in the regular expression using the replace function [modified]
-
Following is the expression , that represents the trimming of the string , which has the white space. function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } function trim( value ) { return LTrim(RTrim(value)); } and am unable to understand the meaning of the expression /\s*((\S+\s*)*)/ Will somebody please make me understand
Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
modified on Tuesday, January 01, 2008 6:38:17 AM
-
Following is the expression , that represents the trimming of the string , which has the white space. function LTrim( value ) { var re = /\s*((\S+\s*)*)/; return value.replace(re, "$1"); } function RTrim( value ) { var re = /((\s*\S+)*)\s*/; return value.replace(re, "$1"); } function trim( value ) { return LTrim(RTrim(value)); } and am unable to understand the meaning of the expression /\s*((\S+\s*)*)/ Will somebody please make me understand
Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
modified on Tuesday, January 01, 2008 6:38:17 AM
the first \s* leaves any number of blank spaces the inner one (\S+\s) matches one more non space characters and a space.. for example.. "asm " or "a " etc. And the outer ((\S+\s)*) matches any number of these words.. in short, this regex breaks a sentence into words.. for ex. " My Name Is ABC XYZ" can be extracted as "My_", "Name_", "Is_", "ABC_" XYZ isn't captured because it does not have a trailing space and so doesn't match (\S+\s) Regards,
Usman Munier Xavor Corporation Pakistan
-
the first \s* leaves any number of blank spaces the inner one (\S+\s) matches one more non space characters and a space.. for example.. "asm " or "a " etc. And the outer ((\S+\s)*) matches any number of these words.. in short, this regex breaks a sentence into words.. for ex. " My Name Is ABC XYZ" can be extracted as "My_", "Name_", "Is_", "ABC_" XYZ isn't captured because it does not have a trailing space and so doesn't match (\S+\s) Regards,
Usman Munier Xavor Corporation Pakistan
i was trying to figure out the solution of trimming. i reached the following expression
function ltrim(s) { var temp = s; alert('hi'); var obj = /^(\s*)([\W\w]*)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function rtrim() { var temp = s; alert('hi'); var obj = /^([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function trim(s) { return rtrim(ltrim(s)); }
I think there is missing something. One more thing , What is the difference between $1 and $2. Please helpYesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
modified on Tuesday, January 01, 2008 8:07:19 AM
-
i was trying to figure out the solution of trimming. i reached the following expression
function ltrim(s) { var temp = s; alert('hi'); var obj = /^(\s*)([\W\w]*)/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function rtrim() { var temp = s; alert('hi'); var obj = /^([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp } function trim(s) { return rtrim(ltrim(s)); }
I think there is missing something. One more thing , What is the difference between $1 and $2. Please helpYesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
modified on Tuesday, January 01, 2008 8:07:19 AM
i have a better solution which i have verified as well on my many application try this. i hope this will solve your problem function TrimWhiteSpaces(s) { s=s.replace(/^\s+|\s+$/g, ""); var myArray=s.split(" "); var outPut=""; for (var i=0;i<myArray.length;i++) { if(myArray[i]!="") { outPut=outPut +" "+ myArray[i]; } } s=outPut; return s; } wish you all the best
Usman Munier Xavor Corporation Pakistan
-
i have a better solution which i have verified as well on my many application try this. i hope this will solve your problem function TrimWhiteSpaces(s) { s=s.replace(/^\s+|\s+$/g, ""); var myArray=s.split(" "); var outPut=""; for (var i=0;i<myArray.length;i++) { if(myArray[i]!="") { outPut=outPut +" "+ myArray[i]; } } s=outPut; return s; } wish you all the best
Usman Munier Xavor Corporation Pakistan
What's the problem in the following chunk of code (i don;t have any problem regarding Left Trim )
function rtrim(s) { var temp = s; var obj = /([\W\w]*)(\s*)$/; if (obj.test(temp)) { temp = temp.replace(obj, '$2'); } return temp }
Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
modified on Wednesday, January 02, 2008 12:14:43 AM