Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

let's say i have a very long string. the string has regular expressions at random locations. can i use regex to find the regex's?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
167 views
Welcome To Ask or Share your Answers For Others

1 Answer

(Assuming that you are looking for a JavaScript regexp literal, delimited by /.)

It would be simple enough to just look for everything in between /, but that might not always be a regexp. For example, such a search would return /2 + 3/ of the string var myNumber = 1/2 + 3/4. This means that you will have to know what occurs before the regular expression. The regexp should be preceded by something other than a variable or number. These are the cases that I can think of:

/regex/;
var myVar = /regex/;
myFunction(/regex/,/regex/);
return /regex/;
typeof /regex/;
case /regex/;
throw /regex/;
void /regex/;
"global" in /regex/;

In some languages you can use lookbehind, which might look like this (untested!):

(?=<^|
|[^sw/]|return|typeof|case|throw|void|in)s*/(?:\/|[^/*
])(?:\/|[^/
])*/

However, JavaScript does not support that. I would recommend imitating lookbehind by putting the portion of the regexp designed to match the literal itself in a capturing group and accessing that. All cases of which I am aware can be matched by this regexp:

(?:^|
|[^sw/]|return|typeof|case|throw|void|in)s*(/(?:\/|[^/*
])(?:\/|[^/
])*/)

NOTE: This regex sometimes results in false positives in comments.

If you want to also grab modifiers (e.g. /regex/gim), use

(?:^|
|[^sw/]|return|typeof|case|throw|void|in)s*(/(?:\/|[^/*
])(?:\/|[^/
])*/w*)

If there are any reserved words I am missing that may be followed by a regexp literal, simply add this to the end of the first group: |keyword

All that remains then is to access the capturing group, using a code similar to the following:

var codeString = "function(){typeof /regex/;}";
var searchValue = /(?:^|
|[^sw/]|return|typeof|case|throw)s*(/(?:\/|[^/*
])(?:\/|[^/
])*/)/g;
    // the global modifier is necessary!
var match = searchValue.exec(codeString); // "['typeof /regex/','/regex/']"
match = match[1]; // "/regex/"

UPDATE
I just fixed an error with the regexp concerning escaped slashes that would have caused it to get only // of a regexp like //hello/

UPDATE 4/6
Added support for void and in. You can't blame me too much for not including this at first, as even Stack Overflow doesn't, if you look at the syntax coloring in the first code block.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...