Ronaldo Killergod Ronaldo Killergod - 1 year ago 65
jQuery Question

Can anyone match this keyword "/test/" from a large string value through RegEx?

I have a string in the form:

var string = /test/subtest/edit?serialno=someserialnumber&pageCacheId=somenumber

I need to extract the substring "/test/" using:

var substring = string.match(...);

I'm using jQuery.

Answer Source

The regex to find the text represented by /test/ in your example is \/.*?\/.

So in JavaScript, using the .match() method:

var substring = string.match(/\/.*?\//)

If you are certain that the value represented by /test/ in your example will always be at the start of the string then use ^ to indicate to only match at the start of the string. Like this:

var substring = string.match(/^\/.*?\//)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download