简体   繁体   English

如何使用 Node.js 将变量传递给正则表达式?

[英]How do I pass a variable into regex with Node js?

So basically, I have a regular expression which is所以基本上,我有一个正则表达式是

var regex1 = /10661\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;

var result=text.match(regex1);
user_activity = result[1].replace(/\s/g, "")
console.log(user_activity);

What I'm trying to do is this我想做的是这个

var number = 1234;
var regex1 = /${number}\" class=\"fauxBlockLink-linkRow u-concealed\">([\s\S]*?)<\/a>/;

but it is not working, and when I tried with RegExp, I kept getting errors.但它不起作用,当我尝试使用 RegExp 时,我不断出错。

You can use RegExp to create regexp from a string and use variables in that string.您可以使用RegExp从字符串创建正则表达式并在该字符串中使用变量。

 var number = 1234; var regex1 = new RegExp(`${number}aa`); console.log("1234aa".match(regex1));

You can build the regex string with templates and/or string addition and then pass it to the RegExp constructor.您可以使用模板和/或添加字符串来构建正则表达式字符串,然后将其传递给RegExp构造函数。 One key in doing that is to get the escaping correct as you need an extra level of escaping for backslashes because the interpretation of the string takes one level of backslash, but you need one to survive as it gets to the RegExp contructor.这样做的一个关键是让 escaping 正确,因为您需要额外级别的 escaping 用于反斜杠,因为字符串的解释需要一级反斜杠,但您需要一个反斜杠才能在它到达 RegExp 构造函数时存活下来。 Here's a working example:这是一个工作示例:

 function match(number, str) { let r = new RegExp(`${number}" class="fauxBlockLink-linkRow u-concealed">([\\s\\S]*?)<\\/a>`); return str.match(r); } const exampleHTML = '<a href="something" class="aaa1234" class="fauxBlockLink-linkRow u-concealed">Some link text</a>'; console.log(match(1234, exampleHTML));

Note, using regex to match HTML like this becomes very order-sensitive (whereas the HTML itself isn't order-sensitive).请注意,像这样使用正则表达式来匹配 HTML 会变得对顺序非常敏感(而 HTML 本身对顺序不敏感)。 And, your regex requires exactly one space between classes which HTML doesn't.而且,您的正则表达式在类之间只需要一个空格,而 HTML 不需要。 If the class names were in a slightly different order or spacing different in the <a> tag, then it would not match.如果 class 名称在<a>标签中的顺序略有不同或间距不同,则它不会匹配。 Depending upon what you're really trying to do, there may be better ways to parse and use the HTML that isn't order-sensitive.根据您真正想要做的事情,可能有更好的方法来解析和使用顺序不敏感的 HTML。

I solved it with the method of Adem,我用Adem的方法解决了,

function escapeRegExp(string) {
    return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}


var number = 1234; 

var firstPart = `<a href="/forum/search/member?user_id=${number}" class="fauxBlockLink-linkRow u-concealed">`

var regexpString = escapeRegExp(firstPart) + '([\\s\\S]*?)' + escapeRegExp('</a>');

console.log(regexpString)


var sample = `<a href="/forum/search/member?user_id=1234" class="fauxBlockLink-linkRow u-concealed"> </a>`

var regex1 = new RegExp(regexpString); 

console.log(sample.match(regex1));

in the first place the issue was actually the way I was reading the file, the data I was applying the match on, was undefined.首先,问题实际上是我读取文件的方式,我正在应用匹配的数据是未定义的。

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

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