简体   繁体   English

正则表达式选择所有内容

[英]Regex select everything except

I'm building a syntax highlighting script and i've got some problems with my regex. 我正在构建语法突出显示脚本,而我的正则表达式遇到了一些问题。 I've been failing for about 2 days now so i need help. 我已经失败了大约2天,所以我需要帮助。

I want to select everything that not /* */ or between them and this is what i got atm, not working tho, but seems close: 我想选择所有不在/* */或它们之间的东西,这就是我得到的atm,虽然不起作用,但看起来很接近:

^(?!(\/\*.*\*\/)$)

An example of i want: 我想要的一个例子:

/* I want to select everything but these comments */
.class-1 {
   font-family: 'SourceCodePro';
   font-size: 16px;
   line-height: 18px;
}
/* I want to select everything but these comments */
.class-2 {
   background-color: rgb(40, 40, 40);
   border: 1px solid rgb(20, 20, 20);
   padding: 10px 15px 10px 15px;
}

I've solved the other regex selections for the rest of the code, but since it's applying to the comments also i need to first select everything but them. 我已经解决了其余代码的其他正则表达式选择,但是由于它已应用于注释,因此我还需要首先选择除它们之外的所有内容。

您可以尝试使用此方法:

!(/\*.+?\*/)

Try this: 尝试这个:

!(\/\*(.+)\*\/)

i cant check it now. 我现在不能检查它。

Take a look at this https://regex101.com/r/cN5aT1/2 看看这个https://regex101.com/r/cN5aT1/2

/^(?!(\/\*(.+)\*\/)).*/gm

It is not working if comments are multiline 如果注释为多行,则无法正常工作

Removing comments | 删除评论| DEMO 演示

For single // and multiline /* */ 对于单//和多行/* */

function filter(str)
{

    var regex =/\/\/.+?(?=\n|\r|$)|\/\*[\s\S]+?\*\//g;

    // if you want for multiline only then
    // use var regex =/\/\*.+?\*\//g;

    var temp = str.replace(regex,'');
    alert(temp);
} 

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

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