skaterdav85 Posted December 23, 2009 Share Posted December 23, 2009 can someone help decipher this for me:stringToTrim.replace(/^\s+|\s+$/g,"");what do these characters do: ^ | $ .... I wasnt too clear on these from the tutorial Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2009 Share Posted December 23, 2009 In this context:^ means "at the beginning of the string" -- notice how it comes before the thing that needs to be at the beginning$ means "at the end of the string" -- notice how it comes after the thing that needs to be at the end| means "or" -- so we are looking for one thing OR the other.Altogether, we are looking for 1 or more space characters (space, tab, newline, etc) at the beginning of the string, or 1 or more space characters at the end of the string, we are looking for them globally (yes, it does matter), and we will replace each match with an empty string.Looks like a left-right trim function to me. Link to comment Share on other sites More sharing options...
skaterdav85 Posted December 23, 2009 Author Share Posted December 23, 2009 could you do something like this then to remove any from the left and right of a string, or does the string have to be special like \s? stringToTrim = stringToTrim(/^ +| $/g,"") Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2009 Share Posted December 23, 2009 You can match anything with a regex. Just keep in mind what you're working with. Shortcuts like \s define character classes that have nothing to do with HTML. When you try to match an HTML entity like you are literally looking for those six characters. So to make that work, you have to treat them as a group. You can do that with parens:re = /^( )+|( )+$/g;s = s.replace(re, ""); But be careful. This literally means at the beginning and the end. If you have a true space or a newline at the end, or right before the first , it won't work. You can do what you want; you'll just need to tweak it.Like, if all you want to do is replace all the characters in your string, and you don't care where they are, just use this:re =/ /g;s = s.replace(re, ""); Link to comment Share on other sites More sharing options...
skaterdav85 Posted December 23, 2009 Author Share Posted December 23, 2009 re = /^( )+|( )+$/g;s = s.replace(re, "");oh ok. ya because i have characters before and after a string that i need to trim. So to replace them, I need to put the in between parenthesis like you did above? Link to comment Share on other sites More sharing options...
jeffman Posted December 23, 2009 Share Posted December 23, 2009 Yes. That tells the repetition operator (the + sign) to treat the six-character entity as a group. Otherwise, it will look for repetitions of the ; character only.Have you tried it? Is it not working? Link to comment Share on other sites More sharing options...
skaterdav85 Posted December 23, 2009 Author Share Posted December 23, 2009 well evidentaly the are only on the outsides of my string, so i just did: string.replace(/ /g,'') I didnt try what you wrote. I just wanted to understand what the parenthesis did. You said: Otherwise, it will look for repetitions of the ; character only.Why would it only look for the semi-colon and not the &,n,b,s, or p characters? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 23, 2009 Share Posted December 23, 2009 Because when you tell it to look for this: +That's saying to look for an ampersand, followed by "n", followed by "b", followed by "s", followed by "p", followed by one or more ";". So that will match this: ;;;;;;but not this: This pattern:( )+Tells it to look for one or more " ". Link to comment Share on other sites More sharing options...
skaterdav85 Posted December 23, 2009 Author Share Posted December 23, 2009 oh ok got. thanks! you're quite the expert at js and php. You can remember all these weird looking regex details. Link to comment Share on other sites More sharing options...
Synook Posted December 24, 2009 Share Posted December 24, 2009 Note that regular expressions as we know them* actually come from Perl *as in PHP and JS. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.