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

javascript - How do I use a template literal in a regular expression? - Stack Overflow

programmeradmin2浏览0评论

I am writing a test which clicks on certain filters and I notice some discrepancies between browsers. For example On major browsers(Chrome, Firefox, Safari) there are check boxes shown exactly in this format: Checkers, Cheese, Salsa, Butter. When I run the test on IE11 those strings are lower case and because it is lower case, my test is failing. To bat that I was hoping to use regular expressions. The function I am using to click those checkboxes is reusable.

I am puzzled because I am trying to pass the string in the regular expression but the test fails. I then went on to create a template literal and someone pass the string in the regular expression that way and the test still failed. My question is, is there a smarter way to pass in a string variable into a regular expression?

The filterPage looks like:

     export default class OrderHistory {
      constructor() {
          this.filter = Selector(
                ".filterCheckbox"

}

async selectFilter(text) {
    await t.click(this.filter.withText(`/${text}`/i));

}

The test page looks like:

 test(“click the cheese filter”, async t => {
   const cheeseFilter = Cheese;

   await filterPage.selectFilter(cheeseFilter);

});

I am writing a test which clicks on certain filters and I notice some discrepancies between browsers. For example On major browsers(Chrome, Firefox, Safari) there are check boxes shown exactly in this format: Checkers, Cheese, Salsa, Butter. When I run the test on IE11 those strings are lower case and because it is lower case, my test is failing. To bat that I was hoping to use regular expressions. The function I am using to click those checkboxes is reusable.

I am puzzled because I am trying to pass the string in the regular expression but the test fails. I then went on to create a template literal and someone pass the string in the regular expression that way and the test still failed. My question is, is there a smarter way to pass in a string variable into a regular expression?

The filterPage looks like:

     export default class OrderHistory {
      constructor() {
          this.filter = Selector(
                ".filterCheckbox"

}

async selectFilter(text) {
    await t.click(this.filter.withText(`/${text}`/i));

}

The test page looks like:

 test(“click the cheese filter”, async t => {
   const cheeseFilter = Cheese;

   await filterPage.selectFilter(cheeseFilter);

});

Share Improve this question asked May 15, 2019 at 19:20 Roosevelt HRoosevelt H 3401 gold badge3 silver badges13 bronze badges 3
  • 2 Possible duplicate of How do you use a variable in a regular expression? – adiga Commented May 15, 2019 at 19:24
  • "[I]s there a smarter way to pass in a string variable into a regular expression"? Yes. new RegExp(string). – Jordan Running Commented May 15, 2019 at 19:25
  • You can use new RegExp(text,"i") – adiga Commented May 15, 2019 at 19:25
Add a ment  | 

1 Answer 1

Reset to default 5

RegExp constructor might do the job if you need to build the regex dynamically, since it can also accept a string.

new RegExp(text, 'i');
发布评论

评论列表(0)

  1. 暂无评论