简体   繁体   English

如何为由变量名称后跟Javascript中的任何字符组成的字符串创建正则表达式?

[英]How do I create a regex for a string made up of a variable name followed by any character in Javascript?

I need to verify "pathnames" of items stored in localStorage (pathname being a string like: abc/localstorage/ualldocs/aalldocs in order to filter whats in localstorage. 我需要验证存储在localStorage中的项目的“路径名”(路径名是一个string如: abc/localstorage/ualldocs/aalldocs ,以便过滤localstorage中的内容。

Right now I'm trying this: 现在我正在尝试这个:

priv.localpath = "abc/localstorage/ualldocs/aalldocs";

for (i in localStorage) {
  if (localStorage.hasOwnProperty(i)) {
    s = new RegExp('\\/' + priv.localpath + '\\/.*$');
    if (s.test(i)) {
      value = localStorage.getItem(i);
      console.log( value );
    } else {
      console.log( "not found: "+i);
    }
  }
}

Which does not work = does not find anything. 哪个不起作用=找不到任何东西。

Question: 题:
How do I create a regex for a string made up of a variable name followed by any character? 如何为由变量名后跟任何字符组成的字符串创建正则表达式?

But the answer is still the same: 但答案仍然是一样的:

Using the constructor allows this for RegExp 使用构造函数允许这用于RegExp

    priv.localpath = "abc/localstorage/ualldocs/aalldocs";

for (i in localStorage) {
  if (localStorage.hasOwnProperty(i)) {
    s = new RegExp(priv.localpath + '\\/*$', "i")
    if (s.test(i)) {
      value = localStorage.getItem(i);
      console.log( value );
    } else {
      console.log( "not found: "+i);
    }
  }
}

Your regex in the loop is always the same, being: new RegExp("\\\\/abc/localstorage/ualldocs/aalldocs\\\\/*$") which will translate into: \\/abc/localstorage/ualldocs/aalldocs\\/*$ 循环中的正则表达式始终是相同的,是: new RegExp("\\\\/abc/localstorage/ualldocs/aalldocs\\\\/*$")将转换为: \\/abc/localstorage/ualldocs/aalldocs\\/*$

I don't really understand: 我真的不明白:

  1. why you regenerate the same regex in the loop; 为什么你在循环中重新生成相同的正则表达式;
  2. what you're trying to achieve. 你想要实现的目标。

However, your answering your question: 但是,您回答了以下问题:

var someString = "something"
var myRegex = new RegExp(someString + '.*'); // creates a regex for the string made up of a variable 'someString' followed by any character: '.*'

暂无
暂无

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

相关问题 javascript正则表达式后面没有特定字符 - javascript regex do not followed by specific character 如何使用正则表达式找到后面没有单词字符的数字? - How do i find the numbers that is not followed by a word character with regex? 如何使用regex使用javascript来获取所有数字,然后是字符串标识符的第一位数字? - How do I use regex to take all numbers followed by the first digit of the identifier of a string using javascript? 如何在正则表达式中将单个S或E字符串中的字符串拆分后跟JavaScript中的数字? - How do I split a string in regex by a single S or E char followed by digits in JavaScript? 如何创建由串联函数参数值和字符串组成的变量名? - How to create a variable name made up of a concatenated functions parameter value and a string? 如何检查字符串是否仅由相同长度的字符组组成? - How do I check if a string is made up exclusively of same-length character groups? Javascript-如何使用基于字符串的变量名称声明变量? - Javascript - How do I declare a variable with the variable name based on a string? 如何使用正则表达式删除字符串中最后一个字母字符后的任何非字母字符? - How do I use regex to remove any non alpha characters after last alpha character in string? 如何使用正则表达式在字符串的末尾获取数字,后跟该字符`:` - How to get a number at the end of a string followed by this character `:` using regex 如何从Javascript / jQuery中的字符串正确创建变量? - How do i properly create a variable from a string in Javascript/jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM