简体   繁体   中英

Using a variable in place of a number in a javascript match function

I'm trying to split a string every n characters using a match function, but I figured that there are other uses to using a variable in a match function.

I know that the code

str.match(/.{0,3}/g);

will give me every three characters in a string, but

var n = 3;
str.match(/.{0,n}/g);

will not.

var n = 3;
str.match(new RegExp(".{0," + n + "}", "g"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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