Jump to content

Regular Expressions


skaterdav85

Recommended Posts

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

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

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

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

Note that regular expressions as we know them* actually come from Perl :)*as in PHP and JS.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...