我正在通过一个项目和替换内部链接去通过url函数。我已经创建了一些我需要的regex,但是我不希望它对动态链接做任何事情,因为我要手动完成它们。
我的正则表达式是:
(href|src)=(?:'|")'/(?!http)(.+?)(?:'|")
我的替换是:
$1="<?php echo url('$2'); ?>"
<link rel='stylesheet' type='text/css' href='/public/css/print.css' media="print"/>
<link rel='stylesheet' type='text/css' href='http://example.com/public/css/print.css' media="print"/>
我希望它忽略以下内容:
<link rel='stylesheet' type='text/css' href='/public/css/print<?php echo 1; ?>.css' media="print"/>
如果有人能提供一个单独的regex和replace,它接受echo并将其放在url函数的字符串连接中,那就加分了!
之类的东西$1="<?php echo url('$2' . $3 . '$4'); ?>"
(?:'|")'/(?!http)([^<]+?)(?:'|")
对于第一部分来说可能已经足够好了。换掉。使用[^<]只匹配不包含左尖括号的字符串。
第二部分
(?:'|")'/(?!http)([^<]+?)(<'?php[^>]*?>)?([^<]+?)(?:'|")
我会用这个:
(href|src)=(?:'|")'/(?!http)((?!php).)+?(?:'|")
解释:
1st Capturing group (href|src)
1st Alternative: href
href matches the characters href literally (case insensitive)
2nd Alternative: src
src matches the characters src literally (case insensitive)
= matches the character = literally
(?:'|") Non-capturing group
1st Alternative: '
' matches the character ' literally
2nd Alternative: "
" matches the characters " literally
'/ matches the character / literally
(?!http) Negative Lookahead - Assert that it is impossible to match the regex below
http matches the characters http literally (case insensitive)
2nd Capturing group ((?!php).)+?
Quantifier: +? Between one and unlimited times, as few times as possible, expanding as needed [lazy]
Note: A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data
(?!php) Negative Lookahead - Assert that it is impossible to match the regex below
php matches the characters php literally (case insensitive)
. matches any character (except newline)
(?:'|") Non-capturing group
1st Alternative: '
' matches the character ' literally
2nd Alternative: "
" matches the characters "