0

I need to parse some JavaScript code in C# and find the regular expressions in that.

When the regular expressions are created using RegExp, I am able to find. (Since the expression is enclosed in quotes.) When it comes to inline definition, something like:

var x = /as\/df/;

I am facing difficulty in matching the pattern. I need to start at a /, exclude all chars until a / is found but should ignore \/.

I may not relay on the end of statement (;) because of Automatic Semicolon Insertion or the regex may be part of other statement, something like:

foo(/xxx/); //assume function takes regex param

If I am right, a line break is not allowed within the inline regex in JavaScript to save my day. However, there the following is allowed:

var a=/regex1def/;var b=/regex2def/;
foo(/xxx/,/yyy/)

I need regular expression someting like /.*/ that captures right data.

4
  • you can use regexhero.net/tester to find regexp for particular text Commented Sep 25, 2013 at 8:19
  • You want a regular expression that matches... regular expressions? Commented Sep 25, 2013 at 8:20
  • Don't you want to match regex modifiers as well? Commented Sep 25, 2013 at 8:24
  • @SimonWhitehead: Yes I need regex to capture jaavscript regexps. Tim: I have no issues with modifiers as of now, but it would be an addon Commented Sep 25, 2013 at 8:28

4 Answers 4

3

You cannot reliably parse programming languages with regular expressions only. Especially Javascript, because its grammar is quite ambiguous. Consider:

a = a /b/ 1
foo = /*bar*/ + 1
a /= 5 //.*/hi

This code is valid Javascript, but none of /.../'s here are regular expressions.

In case you know what you're doing ;), an expression for matching escaped strings is "delimiter, (something escaped or not delimiter), delimiter":

 delim ( \\. | [^delim] ) * delim

where delim is / in your case.

Sign up to request clarification or add additional context in comments.

Comments

0

After several trials with RegexHero, this seems working. /.*?[^\\]/. But not sure if I am missing any corner case.

1 Comment

This definitely fails. Consider /\\/.
0

How about this:

Regex regexObj = new Regex(@"/(?:\\/|[^/])*/");

Explanation:

/       # Match /
(?:     # Non-capturing group:
 \\     # Either match \
 /      # followed by /
|       # or
 [^/]   # match any character except /
)*      # Repeat any number of times
 /      # Match /

5 Comments

When I checked with RegexHero, this expression yields no results
@Tim you probably meant \/(?:\\\/|[^\/])*\/ (no delimiters in C#)
@Jerry: Ah, I read JavaScript and thought he needs a JavaScript regex. In .NET, it's easier :)
@Tim Oh I forgot to remove the extra backslashes for the escaping x3
@KrishnaSarma: I misread your question; I thought you needed a JavaScript regex, not a .NET one. Edited.
0

I think that this may help you var patt=/pattern/modifiers;

•pattern specifies the pattern of an expression •modifiers specify if a search should be global, case-sensitive, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.