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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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 =

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.