简体   繁体   English

如何在Java中的正则表达式模式之前添加文本

[英]How to prepend text before regex pattern in Javascript

I'm trying to prepend a string before a regular expression pattern that matches a style rule using javascript. 我正在尝试在与使用JavaScript的样式规则匹配的正则表达式模式之前添加字符串。 It would be applied for each style rule that matches. 它将应用于匹配的每个样式规则。 What I have thus far doesn't work. 到目前为止,我没有用。 I'm probably doing it wrong. 我可能做错了。

Can someone look at my code and tell me the proper way to do this? 有人可以看看我的代码并告诉我执行此操作的正确方法吗? It works with information provided by WordPress's Customizer. 它可以与WordPress的Customizer提供的信息一起使用。

This is how the script is localized and enqueued: 这是脚本的本地化和排队方式:

function enqueue_jbe_ch_scripts() {
  if( is_home() ) {
    wp_register_script( 'jbe-ch-scripts', plugins_url( 'jbe-ch-scripts.js', __FILE__ ), array( 'jquery' ), '', true );
    wp_enqueue_script( 'jbe-ch-scripts' );
    $parameters = array(
      ... ,
      'display_css' => esc_html( get_theme_mod( 'jbe_custom_css_setting' ) ),
    );     

    wp_localize_script( 'jbe-ch-scripts', 'jbeScriptParams', $parameters );
  }
}

The related Javascript on the front end looks like this: 前端的相关Javascript如下所示:

( function ( $ ) {
  ...
  // Get the content inside of stylesheet
  var jbeUserStylesheet = jbeScriptParams.display_css;

  //var stylesheetContent = jbeUserStylesheet.textContent;

  jbeUserStylesheet.replace( /^([a-z0-9-_]){0,}(#|\.)?[a-z0-9-_,\s]{0,}\s\{([^}])*}$/gmi, '#jbe-custom-html $1' );

  console.log( 'jbeScriptParams.display_css value:' + '\n' + jbeScriptParams.display_css ); // for debugging
}) ( jQuery );

As according to the regex questions rules here at SO, I saved the regex in question at regex101.com 根据SO此处的regex问题规则,我将有问题的regex保存在regex101.com上

I'm interested in Javascript or jQuery solutions or just being pointed in the right direction. 我对Javascript或jQuery解决方案感兴趣,或者只是朝着正确的方向指出。

Update : I left out some very important information in the original question--how the stylesheet prints on the frontend and how the text would be prepended. 更新 :我在原始问题中遗漏了一些非常重要的信息-样式表如何在前端打印以及如何在文本前添加。

The current stylesheet on the frontend: 前端上的当前样式表:

.graybg {
    background-color: #c0c0c0;
}

h2#custom-h2 {
    text-decoration: underline;
}

p {
    color: brown;
}

The new stylesheet: 新样式表:

#jbe-custom-html .graybg {
    background-color: #c0c0c0;
}

#jbe-custom-html h2#custom-h2 {
    text-decoration: underline;
}

#jbe-custom-html p {
    color: brown;
}

You can have regex in a string and then create new regex using new RegExp() . 您可以在字符串中包含正则表达式,然后使用new RegExp()创建新的正则表达式。

Following is a sample depicting the same: 以下是描述相同内容的示例:

 function validate(){ var regex = "_test$"; var _reg = document.getElementById("regex").value; var _val = document.getElementById("value").value; var finalReg = new RegExp(_reg + regex); console.log(finalReg, finalReg.test(_val)); } 
 <input type="text" id="regex" /> <input type="text" id="value" /> <button onclick="validate()">Validate</button> 

I figured out how to prepend the text using javascript .replace . 我想出了如何使用javascript .replace前置文本。 It turns out I wasn't understanding how to use it correctly. 原来我不了解如何正确使用它。 According to the documentation for .replace on the Mozilla Developer Network, the substring (second part after the comma) has placeholders ($n) for each regex capture group. 根据Mozilla开发人员网络上.replace的文档 ,子字符串(逗号后的第二部分)具有每个正则表达式捕获组的占位符($ n)。

Thus, I adjusted my regular expression to the following: 因此,我将正则表达式调整为以下内容:

/^(([a-z0-9-_]{0,})(#|\\.)?([a-z0-9-_,\\s]{0,})){0,}(\\s\\{([^}])*})$/gmi;

Here, I have a total of five capture groups, with the first wrapping the second, third and fourth, followed by the fifth. 在这里,我总共有五个捕获组,第一个捕获了第二个,第三个和第四个,然后是第五个。 In .replace, I printed the whole regex to the page with $1 for the entire first four capture groups and $5 for the last. 在.replace中,我将整个正则表达式打印到页面上,其中前四个捕获组为$ 1,最后一个捕获组为$ 5。 I prepended the string I needed to the beginning of the substring. 我将所需的字符串放在子字符串的开头。 My code now looks like the following in total: 现在,我的代码总计如下所示:

// Prepend the string #jbe-custom-html to each style rule in style#jbe-ch-frontend

// Assign jbeScriptParams.display_css php variable to another variable
var jbeUserStylesheet = jbeScriptParams.display_css;

/* from the page String.prototype.replace() */
/* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace */
var regexp = /^(([a-z0-9-_]{0,})(#|\.)?([a-z0-9-_,\s]{0,})){0,}(\s\{([^}])*})$/gmi;

var newstr = jbeUserStylesheet.replace( regexp, '#jbe-custom-html $1$5' );

//console.log( newstr ); // for debugging

// Append the new style rules to the end of style#jbe-ch-frontend using jQuery
$( "style#jbe-ch-frontend" ).append( "\n" + "/* User entered styles */" + "\n\n" + newstr );

The actual regular expression is another story. 实际的正则表达式是另一个故事。 I still will need to change it some time in the future. 我将来仍需要更改它。 When I was debugging it on regex101, the debugger found a lot of backtracking. 当我在regex101上调试它时,调试器发现很多回溯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM