Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将递归PHP正则表达式转换为JavaScript_Php_Javascript_Regex - Fatal编程技术网

将递归PHP正则表达式转换为JavaScript

将递归PHP正则表达式转换为JavaScript,php,javascript,regex,Php,Javascript,Regex,我需要帮助在JavaScript中复制此PHP正则表达式: 这是不可能的 您无法将此正则表达式转换为JavaScript风格,因为它使用了JavaScript正则表达式引擎不支持的递归(?R) 我建议采用另一种方法。我假设您希望删除尖括号内的所有内容,包括周围的括号,除非这些括号位于..块中。对吗?最好是JavaScript正则表达式(它甚至不支持lookbehind断言)我能为您做的是: result = subject.replace(/<(?!\/code)[^<>]*&

我需要帮助在JavaScript中复制此PHP正则表达式:

这是不可能的

您无法将此正则表达式转换为JavaScript风格,因为它使用了JavaScript正则表达式引擎不支持的递归
(?R)

我建议采用另一种方法。我假设您希望删除尖括号内的所有内容,包括周围的括号,除非这些括号位于
..
块中。对吗?最好是JavaScript正则表达式(它甚至不支持lookbehind断言)我能为您做的是:

result = subject.replace(/<(?!\/code)[^<>]*>\s*(?!(?:(?!<code>)[\s\S])*<\/code>)/g, "");
这确保了我们仅在下一个
/
标记是开头的
标记,而不是结尾的
标记(或者根本没有这样的标记)时匹配标记

所以它转变了

This <b> is bold </b> text, 
but we want <code> these <i> tags <b> here </b> to remain </i> </code> 
while those <b> can be deleted</b>.
这将给出结果

This is bold text, 
but we want these <i> tags <b> here </b> to remain </i> 
while those can be deleted.
这是粗体文本,
但我们希望这些标签留在这里
而那些是可以删除的。

如果
code
标记可以嵌套,那么这些正则表达式都不起作用。

如果您想在JavaScript中实现这一点,我的猜测是,您可能正在一个环境中工作,您手头已经有一套完整的HTML解析和遍历工具——浏览器DOM

如果是这样的话,正则表达式不是标记标记的理想工具的一般好的建议在这里加倍应用,并且您可能会考虑做其他的事情。 将一段给定的标记转换成一个表单,您可以使用DOM接口对其进行操作,这非常简单:

var working = document.createElement('div');  //create a new empty element
working.innerHTML = sourceToSanitize;         //put your HTML source inside it
var sanitized = sanitize(working);            //call sanitization function!
现在您只需要一个
sanitize
函数,您可以调用该元素,该元素将遍历DOM树中的每个节点,并返回一段转换后的HTML

类似的方法可能会奏效:

function sanitize(emt) {
    if(emt.nodeType == 3)     // terminal cond #1: just return text nodes
        return emt.textContent;
    if(emt.nodeType != 1)     // terminal cond #2: non-text/element nodes yield null
        return null;
    if(emt.tagName=='code' || emt.tagName=='CODE') //#3: code tags returned untouched
        return outerHTML(emt);

                              // recurse over all child nodes
    var schf = [], // *S*anitized *C*hild *H*TML *F*ragments
        children = emt.childNodes;
    for(var i=0,z=children.length; i<z; i++) 
        schf.push(sanitize(children[i]));
    return schf.join('');     // smoosh results together and serve fresh!
}

function outerHTML(emt) {
    if(emt.outerHTML) return emt.outerHTML;
    var tmp = document.createElement('div');
    tmp.appendChild(emt.cloneNode(true));
    return tmp.innerHTML;
}
功能清理(emt){
if(emt.nodeType==3)//终端条件#1:只返回文本节点
返回emt.textContent;
if(emt.nodeType!=1)//终端条件#2:非文本/元素节点产生null
返回null;
if(emt.tagName='code'| | emt.tagName=='code')/#3:返回的代码标记未触及
返回外部TML(emt);
//在所有子节点上递归
var schf=[],//*S*aninited*C*hild*H*TML*F*片段
childrends=emt.childNodes;

对于(var i=0,z=childrence.length;它应该是JavaScript-jQuery不做regext这个regex肯定不做您所说的事情。
[code]
匹配单个字母,可以是c、o、d或e。至少必须对括号进行转义。相反,为什么不定义所需正则表达式的参数,并向我们展示您所做的尝试,我们将能够帮助您。正则表达式是错误的,我更新了。是否可以实现递归(?R)用另一种方式在javascript中?你不能做递归,但如果你愿意接受我的解决方案的局限性,那是你在javascript中单独使用正则表达式得到的下一个最好的解决方案。不过,解析器会更加防弹。1.是否可以去掉
标记,只把html放在里面?.2.万一,放在
里面。..
如果您发现其他的
,则无法删除这些?我不完全确定您所说的1是什么意思。是否要保留正则表达式现在的行为并另外删除代码标记?大约2.,这就是正则表达式失败的原因。嵌套的代码标记将导致失败,因为嵌套是您确实需要递归的事情。因此如果无法保证代码标记永远不会嵌套,您不能在JavaScript中使用正则表达式。请举例说明:谢谢。。。
This is bold text, 
but we want these <i> tags <b> here </b> to remain </i> 
while those can be deleted.
var working = document.createElement('div');  //create a new empty element
working.innerHTML = sourceToSanitize;         //put your HTML source inside it
var sanitized = sanitize(working);            //call sanitization function!
function sanitize(emt) {
    if(emt.nodeType == 3)     // terminal cond #1: just return text nodes
        return emt.textContent;
    if(emt.nodeType != 1)     // terminal cond #2: non-text/element nodes yield null
        return null;
    if(emt.tagName=='code' || emt.tagName=='CODE') //#3: code tags returned untouched
        return outerHTML(emt);

                              // recurse over all child nodes
    var schf = [], // *S*anitized *C*hild *H*TML *F*ragments
        children = emt.childNodes;
    for(var i=0,z=children.length; i<z; i++) 
        schf.push(sanitize(children[i]));
    return schf.join('');     // smoosh results together and serve fresh!
}

function outerHTML(emt) {
    if(emt.outerHTML) return emt.outerHTML;
    var tmp = document.createElement('div');
    tmp.appendChild(emt.cloneNode(true));
    return tmp.innerHTML;
}