我想知道以哪种方式可以从那段代码中获取“ widget”表达式:
blablablalbadsj kds {{widget type="Magento\FooBard\Block\Widget\Script" wysywig_text="<img src='{{media url='wysiwyg/something.png'}}' alt='' />"}} ksakkdkkcxz {{media url='wysiwyg/something2.png'}}
最后,我想拥有:
{{widget type="Magento\FooBard\Block\Widget\Script" wysywig_text="<img src='{{media url='wysiwyg/something.png'}}' alt='' />"}}
我一直在考虑这个问题,并且有这样的正则表达式:
/\{\{widget(.*?)\}}/
但这是行不通的,它只是匹配:
{{widget type="Bold\Gtm\Block\Widget\Script" wysywig_text="<img src='{{media url='wysiwyg/compare_brightness.png'}}
没有:alt =“''/>”
我的建议是不要为此使用Regexps,因为该字符串不是常规字符串。因此,尝试使用正则表达式进行解析将非常困难。
var str =`blablablalbadsj kds {{widget type="Magento\FooBard\Block\Widget\Script" wysywig_text="<img src='{{media url='wysiwyg/something.png'}}' alt='' />"}} ksakkdkkcxz {{media url='wysiwyg/something2.png'}}`
var startWdigetPos = str.indexOf("{{widget"),endWidgetPost = 0;
const regex = /{{|}}/mg;
var openedTags=0;
var closedTags=0;
while ((m = regex.exec(str.substr(startWdigetPos))) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
if (m[0]=="{{"){
openedTags++;
}else if (m[0]="}}"){
closedTags++;
}
if (openedTags === closedTags){
endWidgetPost = regex.lastIndex ;
break;
}
}
if (endWidgetPost){
console.log(str.substr(startWdigetPos,endWidgetPost));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句