使用preg_replace我想修改HTML页面上所有发生的内联javascript(在脚本标记内),如果脚本包含'jQuery'并且不包含'defer'。修改是在现有脚本周围添加额外的脚本。
例如,这是HTML输出的一部分:
<script type="text/javascript">//Should not match, because of 'defer'
function defer(method) {
if (window.jQuery) {
method();
} else {
setTimeout(function() { defer(method) }, 50);
}
}
</script>
<script type="text/javascript">//Should match because of 'jQuery'
jQuery(document).ready(function(){
//some code
});
</script>
<script type="text/javascript">//Should not match, because of no 'jQuery'
window._wpemojiSettings = {"baseUrl"....."}
</script>
<script type="text/javascript">//Should match because of 'jQuery' further down within the script tag
if(typeof gf_global == 'undefined') //some other code
jQuery(document).bind(function(){
//some code
});
</script>
现在我来到这里:
$buffer = preg_replace('#<script type="text\/javascript">(.*?jQuery.*?)<\/script>#is', '<script type="text/javascript">/*Additional code around it*/$1/*Additional code*/</script>', $buffer);
然而,当脚本标记中没有出现jQuery时,HTML的其余部分也会被考虑在内,直到'jQuery ... / script&gt;'发生。
有关于此的任何想法吗?
非常感谢!
答案 0 :(得分:0)
这个解决方案怎么样(经过测试):
import {AbstractControl} from '@angular/forms';
export const nameValidator = (control: AbstractControl): {[key: string]: boolean} => {
const firstName = control.get('firstName');
const lastName = control.get('lastName');
if (!firstName || !lastName) {
return null;
}
return firstName.value && lastName.value ? null : { required: true };
};