-1

I was working on a RegExp and found out that if the string contains dash aa-bb.js and the query string part m=123, it will cause the browser to crash, apart from not being the best Regular Expression for this purpose, I'd like to know why does it work for https://example.org/aa.js and not for https://example.org/js/aa-bb.js?m=123

var re = /src=[\'|\"]([ -0-9a-zA-Z:]*[ 0-9a-zA-Z;]*)*[\'|\"]/g;
var result = re.exec("<script src='https://example.org/js/aa-bb.js?m=123'></script>");
console.log(result)
Ramtin Gh
  • 1,035
  • 2
  • 11
  • 31
  • 1
    regex crash .. backtracking issues? look at [fixing-catastrophic-backtracking-in-regular-expression](https://stackoverflow.com/questions/45463148/fixing-catastrophic-backtracking-in-regular-expression) – Patrick Artner May 05 '18 at 17:53

1 Answers1

1

Besides your regex has other problems,
your error is this (which probably caused the crash):

Error: Regex Construction .. 

Invalid range end in character class

     src= [\'|\"] 
     (                             # (1 start)
          [ -0-<<<HERE>>>9a-zA-Z:]* 
          [ 0-9a-zA-Z;]* 
     )*                            # (1 end)
     [\'|\"]

In the class, the range <space> to 0, then a range operator without the starting character.


Other problems:

This character class [\'|\"] has an alternation literal in it.
Do you intend that?

Also, you have optionally-quantified optional-quantifiers without specifying
something non-optional.
What makes this really bad is when you have classes which content overlap.
That is a recipe for backtracking overflow.

Example ([abcD]*[abc]*)*


Overall, the regex you probably need is this src=(['"])(?:(?!\1)[\S\s])*\1