最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to concatenate strings for building a regex? - Stack Overflow

programmeradmin3浏览0评论

I'm using JavaScript and i have a question about regex. I haven't found exactly what i'm asking so here it goes.

I have a super long regex and i would like to split it into a bunch of more smaller strings to make clear what the regex is validating.

I know that it is possible to split the long regex into smaller ones by doing something like (and this way actually works)

const patternOne = /(\w+:{0,1}\w*@)?/;
const patternTwo = /([a-zA-Z0-9.-]+)/;

const pattern = new RegExp(`^${patternOne.source}${patternTwo.source}$`, 'i');

But i would like to omit the .source and do something more simpler like

const patternOne = '(\w+:{0,1}\w*@)?';
const patternTwo = '([a-zA-Z0-9.-]+)';

const pattern = new RegExp(`/^${patternOne}${patternTwo}$/`, 'i');

Just using the strings

But when i do for example pattern.test(myString) i get an error that the regex is invalid.

Maybe i forgot to escape one of my characters? Or is not possible to use just the strings?

I'm using JavaScript and i have a question about regex. I haven't found exactly what i'm asking so here it goes.

I have a super long regex and i would like to split it into a bunch of more smaller strings to make clear what the regex is validating.

I know that it is possible to split the long regex into smaller ones by doing something like (and this way actually works)

const patternOne = /(\w+:{0,1}\w*@)?/;
const patternTwo = /([a-zA-Z0-9.-]+)/;

const pattern = new RegExp(`^${patternOne.source}${patternTwo.source}$`, 'i');

But i would like to omit the .source and do something more simpler like

const patternOne = '(\w+:{0,1}\w*@)?';
const patternTwo = '([a-zA-Z0-9.-]+)';

const pattern = new RegExp(`/^${patternOne}${patternTwo}$/`, 'i');

Just using the strings

But when i do for example pattern.test(myString) i get an error that the regex is invalid.

Maybe i forgot to escape one of my characters? Or is not possible to use just the strings?

Share Improve this question edited Aug 11, 2017 at 18:22 Sagar V 12.5k8 gold badges45 silver badges70 bronze badges asked Aug 11, 2017 at 18:16 user6655656user6655656 1
  • 1 You don't put the forward slashes around a regex defined with the RegExp function. – James Commented Aug 11, 2017 at 18:20
Add a ment  | 

2 Answers 2

Reset to default 8

You need to double escape instead of single escape that is you have to use 2 slashes.

Also you don't have to add / at the start and end in RegExp function.

const pattern = new RegExp(`/^${patternOne}${patternTwo}$/`, 'i');
                           ^^                           ^^ -> remove those

It will be added automatically

const patternOne = '(\\w+:{0,1}\\w*@)?';
const patternTwo = '([a-zA-Z0-9.-]+)';

const pattern = new RegExp("^" + patternOne + patternTwo + "$", 'i');
console.log(pattern);

The closest solution to you code is:

const patternOne = String.raw`(\w+:{0,1}\w*@)?`;
const patternTwo = String.raw`([a-zA-Z0-9.-]+)`;

const pattern = new RegExp(`^${patternOne}${patternTwo}$`, 'i');
发布评论

评论列表(0)

  1. 暂无评论