Jump to content

Help with Javascript Regular Expressions (new to regexp)


shortysbest

Recommended Posts

I'm new to regexp, and i need help learning how to use it correctly.

 

What would the equation look like if I wanted it to do this:

If the string is:

 

#!/profile&id=1

 

What would the expression be to return just "profile". Having it search after #!/ and &.

 

The same for if I had a string like this:

 

#!/profile&id=1&tab=home

 

if i wanted to return just "profile" in one variable, and "tab" in another variable

 

If you could sort of a explain the parts of the equation so I would know how to do it myself, that would be wonderful.

Thanks.

http://www.regular-expressions.info/

/#!([^&]+)&id=\d+(&([^=]+)=home)/.match("#!/profile&id=1&tab=home")

- /.../ is the expression

- (...) gets captured for later

- [^...] matches characters not included in the list

- \d matches a digit

- + means "one or more of the previous thing"

 

I get the impression that the string can vary much more than as in the examples you gave...

Thanks, and yes, "profile" could be a number of things, same with tab, that could be a number of things, and there could also be more variables.

 

#!/profile&id=1&tab=home&more_stuff=blahblah

 

etc. What the expression would need to do is to select letters and numbers between #!/ and & and =

 

 

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.