简体   繁体   中英

Shouldn't this RegExp work?

testString = "something://something/task?type=Checkin";

patt = new RegExp("something\/(\w*)\?");
match = patt.exec(testString);
document.querySelector('#resultRegexp').innerHTML = match[1];

I want to capture task So shouldn't this RegExp work?

I am grabbing any alphanumeric character up until the question mark... and capturing it.

http://jsfiddle.net/h4yhc/2/

You would need to escape the slash in regex literals, and the backslash in string literals which you create regexes from:

var patt = /something\/(\w*)\?/g;
// or
var patt = new RegExp("something/(\\w*)\\?", 'g');

I strongly recommend the first version, it is more readable.

I think this would be enough: (\\w*)\\? , since / is not captured by \\w and the only ? in the string is after your target string.

This is what you need:

patt = new RegExp(".*/(\\w*)\\?");

http://jsfiddle.net/FJcfd/

try with this: var pat = /something:\\/\\/(?:[^\\/]+\\/)+(\\w+)\\?(\\w+=\\w+)/;

it can match string such as:

something://something/task?type=Checkin
something://something/foo/task?type=Checkin
something://something/foo/bar/task1?type3=Checkin4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM