Reference to nested groups in JavaScript using string replacement using regular expression

Due to the way jQuery deals with script tags, I found it necessary to do some manipulation using regular expressions (yes, I know ... not an ideal tool to work with). Unfortunately, it seems to me that my understanding of how the captured groups work in JavaScript is wrong, because when I try to do this:

var scriptTagFormat = /<script .*?(src="(.*?)")?.*?>(.*?)<\/script>/ig;

html = html.replace(
    scriptTagFormat, 
    '<span class="script-placeholder" style="display:none;" title="$2">$3</span>');

Script tags are replaced with spaces, but the resulting attribute is titleempty. Shouldn't $2match the contents of the attribute of the srcscript tag?

+3
source share
5 answers

, , , :

var scriptTagFormat = /<script\s+((.*?)="(.*?)")*\s*>(.*?)<\/script>/ig;

html = html.replace(
    scriptTagFormat, 
    '<span class="script-placeholder" style="display:none;" $1>$4</span>');

span. . , DOM, HTML, .

0

; . , # 1, src="value", # 2, value.

+4

.*? , , == > src .*?. ? , .

. @morja , , .*? src.

: /<script (?:.*?(src="(.*?)"))?.*?>(.*?)<\/script>/ig

, rubular ( ​​ )

, , (?:)

/<script (?:.*?(?:src="(.*?)"))?.*?>(.*?)<\/script>/ig

$1 $2.

+1

:

/<script (?:(?!src).)*(?:src="(.*?)")?.*?>(.*?)<\/script>/ig

: rubular

stema, .*? . lookahead (?:(?!src).)* src.

.*? :

/<script (?:.*?src="(.*?)")?.*?>(.*?)<\/script>/ig

: rubular

+1

Could you post the html you are extracting? Your code works fine with a simple example: jsfiddle (warning: warning window)

My first assumption: one of the script tags does not have src meaning that you are left with one capture group (the contents of the script).

0
source

All Articles