English 中文(简体)
贾瓦文——插图匹配中的使用变量
原标题:JavaScript - Use variable in string match

我发现几个类似的问题,但我没有帮助。 因此,我有这个问题:

var xxx = "victoria";
var yyy = "i";
alert(xxx.match(yyy/g).length);

我不知道如何在指挥上通过变数。 请提供帮助。 谢谢。

最佳回答

虽然配对功能并不接受将字面描述为reg形,但你可以使用彩色标的构造者,并将之传递给强。 相应职能:

var re = new RegExp(yyy,  g );
xxx.match(re);

您所需要的任何旗帜(如/g)可进入第二个参数。

问题回答

页: 1 如果您的格局正在显现,

var xxx = "victoria";
var yyy = "i";
var rgxp = new RegExp(yyy, "g");
alert(xxx.match(rgxp).length);

如果格局不是动态的扼杀:

var xxx = "victoria";
var yyy = /i/g;
alert(xxx.match(yyy).length);

例如:

let myString = "Hello World"
let myMatch = myString.match(/H.*/)
console.log(myMatch)

let myString = "Hello World"
let myVariable = "H"
let myReg = new RegExp(myVariable + ".*")
let myMatch = myString.match(myReg)
console.log(myMatch)

<>Example> 1. 在座标中找到 number号

var word= Web Development Tutorial ;
var vowels= [aeiou] ; 
var re = new RegExp(vowels,  gi );
var arr = word.match(re);
document.write(arr.length);

for me anyways, it helps to see it used. just made this using the "re" example:

var analyte_data =  sample- +sample_id;
var storage_keys = $.jStorage.index();
var re = new RegExp( analyte_data, g );  
for(i=0;i<storage_keys.length;i++) { 
    if(storage_keys[i].match(re)) {
        console.log(storage_keys[i]);
        var partnum = storage_keys[i].split( - )[2];
    }
}

下面是比较简单、有效的方式,像在联合材料中操作者一样。 enjoy!

const likePattern =  %%i%%%%%a ;
const regexp = new RegExp(likePattern.replaceAll( % , .* ),"i");
console.log("victoria".match(regexp));




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签