I have a regular expression like this :
/(?:<script\s+[^>]*?src="([^"]+)"[^>]*><\/script>)|(?:<link\s+[^>]*?href="([^"]+)"[^>]*>)/g
I want to replace the "src" in <script>
tag, or "href" in <link />
tag with javascript with this regexp.
the code like this :
html.replace( /(?:<script\s+[^>]*?src="([^"]+)"[^>]*><\/script>)|(?:<link\s+[^>]*?href="([^"]+)"[^>]*>)/g, function( m, n ) {
return m.replace( n, 'other url' );
}
It is working fine with <script>
tag but not link tag. coz the regexp still set the first match in ([^"]+) in to the arguments, so that the "n" param is undefined as it is not match <script>
tag. if the regexp match a <link>
tag, the code must be modified to :
html.replace( /(?:<script\s+[^>]*?src="([^"]+)"[^>]*><\/script>)|(?:<link\s+[^>]*?href="([^"]+)"[^>]*>)/g, function( m, n ) {
return m.replace( arguments[ 2 ], 'other url' );
}
Is there any way to make the regexp not capture the first match if it does not match a <script>
tag?