Regular expression difference between ((?: [^ \ "]) *) And ([^ \"] *)

What is the difference between these regular expressions: replaceable?

((?:[^\"])*) ([^\"]*) 

background to this question:

The WYSIWYG javascript editor (tinymce) cannot parse my html code in Firefox (23.0.1 and 25.0a2), but it works in Chrome.

I found that regex is to blame:

 attrRegExp = /([\w:\-]+)(?:\s*=\s*(?:(?:\"((?:[^\"])*)\")|(?:\'((?:[^\'])*)\')|([^>\s]+)))?/g; 

which I modified by replacing

 ((?:[^\"])*) 

with

 ([^\"]*) 

and

 ((?:[^\'])*) 

with

 ([^\']*) 

the resulting regular expression works in both browsers for my test case

 attrRegExp = /([\w:\-]+)(?:\s*=\s*(?:(?:\"([^\"]*)\")|(?:\'([^\']*)\')|([^>\s]+)))?/g 

can anyone highlight this?

my test data, which only work with the modified regular expression, is a large image> 700 kb, for example:

 var testdata = '<img alt="" src="data:image/jpeg;base64,/9j/4AAQSkZJRgA...5PmDk4FOGOHy6S3JW120W1uCJ5M0PBa54edOFAc8ePX/2Q==">' 

do something like this for testing:

 testdata.match(attrRegExp); 

especially when the test data is large, the unmodified regular expression most likely fails in firefox.

You can find the jsfiddle example here :

+6
source share
1 answer

As a result, there should be no difference. So you should be fine.

However, there may be a big difference in how the RegExp engines will handle these two expressions, and in the case of Firefox / Safari, you just proved you really are :)

Firefox uses WebKit / JavaScriptCore YARR. YARR imposes an arbitrary, artificial limit that falls into the non-exciting group option

 // The below limit restricts the number of "recursive" match calls in order to // avoid spending exponential time on complex regular expressions. static const unsigned matchLimit = 1000000; 

As with Safari.

See the corresponding Webkit error and the corresponding Firefox error and a good test case comparing the different types of expressions that someone has compiled.

+5
source

Source: https://habr.com/ru/post/953779/


All Articles