Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing beginning SPACEs from string use RegExp 2

Status
Not open for further replies.

bitplayer

Programmer
Feb 22, 2006
10
0
0
US
Code:
QstrVal.replace(/^\s+(.+)/, "$1"); // replace one or more SPACEs at start of string with nothing
QstrVal.replace(/(.+)\s+$/, "$1"); // replace one or more SPACEs at end of string with nothing

I wanted to come up with a small piece of code for removing preceding and trailing spaces. From what I have read in my JavaScript O'Reilly, I can understand why the second line would not do what I want. But I really don't understand why the first line keeps leaving ONE space if QstrVal has a value of, for example, " test". Won't the \s+ match all preceding white space?
 
This function accepts a value, strips leading and trailing spaces and tests to see if it has any length when it is done and returns true or false.
Code:
function not_empty(value) {
  //Strips leading and trailing whitespace and tests if anything remains.
  var re = (value.replace(/^\s+|\s+$/g,'').length > 0)?true:false;
  return re;
}

You can modify this to strip leading and trailing spaces only something like this.

myval = myval.replace(/^\s+|\s+$/g,'');

Stamp out, eliminate and abolish redundancy!
 
I didn't really look at your code to see what was wrong, but if I was going to do this here is how I would:
[small]I put in the *'s in the alerts to show where the string starts and ends[/small]
Code:
<script type="text/javascript">
var a = "        test         ";
alert("*" + a.replace(/^\s+/, "") + "*");
alert("*" + a.replace(/\s+$/, "") + "*");
</script>

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
kudos to niteowl for putting it in an "or" condition. I would have strung it out into 2 replace statements.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
o'niteowl RULES!!!
headbang.gif




*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
I cannot take credit for it.
I am horrible with regular expressions (someday I will read up on them) but have been keeping an eye out for useful ones and building up my library. :)
I love compact code.


Stamp out, eliminate and abolish redundancy!
 
I take back your star then. [lol]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Aww phutz! I get so few these days.
Seems the more help someone needs the less grateful they are for it.
Good thing I do it just for kicks and personal knowledge.


Stamp out, eliminate and abolish redundancy!
 
Code:
myval = myval.replace(/^\s+|\s+$/g,'');
theniteowl, that's very very nice! My problem was I didn't realize that the replace method doesn't operate on its string, so you got to do myval = myval.replace(). It wasn't leaving just one space, it wasn't doing anything because I wasn't saving the results. Duh... :~/
 
Hehe, one of us should have spotted that.
I saw the regexp in there and just skipped right past it as I do not know enough of them to evaluate the expressions yet and just skipped over the code knowing I had something saved away here that would do the trick.

Glad it's working for ya and it's probably good to have expressions that trim just from the front and just from the end in your own code library anyway as they could come in handy at some point.




Stamp out, eliminate and abolish redundancy!
 
I should have put the | in my response. [cry]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top