【问题标题】:Search for a pattern in string Js在字符串 Js 中搜索模式
【发布时间】:2022-01-23 01:24:29
【问题描述】:

我正在尝试编写一些代码,我尝试过但不起作用,所以我有一个字符串,我希望我的代码在其中搜索一个模式。例如,如果“你好,这是一个测试”,我想在这个字符串中搜索“etet”模式和第一次迭代。在这种情况下,它将是: 'hello, this is atest' 之后,其他字符将被替换通过 '-' 像这样:'-e-----t---------e-t'。我试过正则表达式:

string = string.replace(/[^eEtT]/g, '-')

但它给出了每个字母,而不是唯一的模式。 感谢您的帮助。

【问题讨论】:

  • 这个console.log('hello, this is a test'.replace(/[^eEtT]/g, '-'));-e-----t---------te-t 这不是预期的吗?
  • 中间的't'不应该在这里
  • @Drakeee0909 为什么不应该在那里?您将替换除et 之外的所有内容。所以t 应该留下来。
  • 是的,这个代码是的,但是对于我想要的,它不应该在那里
  • 不清楚你真正想要什么。

标签: javascript regex


【解决方案1】:

这个正则表达式:

/(^.*?)(e)(.*?)(t)(.*?)(e)(.*?)(t)(.*)/gm

(^.*?) 匹配字符串的开头,包含 0 个或多个任意字符,直到它到达...

(e)一个e,然后

(.*?) 0 个或多个任意字符,直到到达

(t)一个t。

等等……

hello-this-is-the-test 匹配的组是:

0-1 h
1-2 e
2-6 llo-
6-7 t
7-16    his-is-th
16-17   e
17-18   -
18-19   t
19-22   est

https://regex101.com/r/FPRCT1/1

【讨论】:

    【解决方案2】:

    您可以使用 2 个捕获组,第一个捕获 e,然后捕获下一个 t 在replace的回调中,检查是否有捕获组。

    (e)[^t]*(t)|.
    

    Regex demo

    替换为第 1 组和第 2 组,并重复添加 _ 以获得两者之间匹配的字符数。

    const regex = /(e)[^t]*(t)|./gi;
    const s = "hello, this is a test.";
    const result = s.replace(regex, (m, g1, g2) =>
      g1 ? g1 + "-".repeat(m.length - 2) + g2 : "-"
    );
    console.log(result);

    【讨论】:

      【解决方案3】:

      老实说,我看不出这有什么实际用途,但如果你想要的话,我猜是肯定的。 这样的事情可能会有所帮助:

      function stringSearch(string, search, caseSensitive=true) {
          if(caseSensitive==false) {
              string = string.toLowerCase();
              search = search.toLowerCase();
          }
      
          let cache = ""; // this will store the characters we have found
          let complete = false; // whether we found the search string or not
      
          let pos = 0; // current position in search
      
          for(i=0;i<string.length;i++) { // loop through every character in the string
              if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
                  cache += search[pos]; // if so, add the character to the cache
                  pos++; // add 1 to the position
                  if(cache == search) { // check if the cache is equal to the search string
                      complete = true; // set complete to true
                      break; // break the loop
                  }
              }
          }
      
          return complete;
      }
      
      
      // examples:
      console.log("'helolo' has 'hello': "+stringSearch('helolo', 'hello')); // true
      console.log("'helo' has 'hello': "+stringSearch('helo', 'hello')) // false
      console.log("'HELLO' has 'hello': "+stringSearch('HELLO', 'hello')); // false
      console.log("'HELLO' has 'hello' (incase sensitive): "+stringSearch('HELLO', 'hello', false)); // true

      它的工作方式非常简单。它有一个缓存变量。该变量将保存与搜索字符串匹配的每个字符。

      所以,它遍历大字符串中的每个字符。然后有另一个变量将当前位置存储在搜索字符串中。如果大字符串中的当前字符与搜索字符串中的当前字符匹配,则将其添加到缓存变量中,并将搜索字符串的位置加一。然后它运行一个 if 语句来查看缓存是否等于搜索字符串。如果是,则返回 true。如果你愿意,你可以让函数返回缓存而不是布尔值。

      如果你想要破折号,你可以使用这个代码:

      function stringSearch(string, search, caseSensitive=true) {
          if(caseSensitive==false) {
              string = string.toLowerCase();
              search = search.toLowerCase();
          }
      
          let cache = ""; // this will store the characters we have found
          let complete = false; // whether we found the search string or not
          let returnValue = "";
      
          let pos = 0; // current position in search
      
          for(i=0;i<string.length;i++) { // loop through every character in the string
              if(string[i] == search[pos]) { // check if it matches the current character in the searchkey
                  cache += search[pos]; // if so, add the character to the cache
                  returnValue += search[pos]; // add the character to the return value
                  pos++; // add 1 to the position
                  if(cache == search) { // check if the cache is equal to the search string
                      complete = true;
                      break; // break the loop
                  }
              } else {
                  returnValue += "-"; // add a - to the return value
              }
          }
      
          if(complete) {
              return returnValue; // return the result
          } else {
              return;
          }
      }
      
      
      // examples:
      console.log("'helolo' has 'hello': "+stringSearch('helolo', 'hello')); // true
      console.log("'helo' has 'hello': "+stringSearch('helo', 'hello')) // false
      console.log("'HELLO' has 'hello': "+stringSearch('HELLO', 'hello')); // false
      console.log("'HELLO' has 'hello' (incase sensitive): "+stringSearch('HELLO', 'hello', false)); // true
      console.log("'hello, this is a test' has 'etet': "+stringSearch("hello, this is a test", "etet"));

      它的工作原理几乎相同。唯一的区别是添加了一个存储破折号和文本的 returnValue 变量。每次将一个字符添加到缓存中时,它也会添加到 returnValue。但是,如果没有将值添加到缓存中,则会将 - 添加到 returnValue。

      this 没有捕捉到中间 't' 的原因是,当它越过那个 't' 时,它仍在寻找一个 'e'。

      【讨论】:

      • 这假定输入和模式都在 ASCII 范围内,并且不适用于具有更高 unicode 代码点的字符。
      • OP 的要求似乎不是这样。
      • 此外,这与表情符号以及您实际需要的任何东西都非常适合。
      • 我做了一点测试。它适用于非ASCII字符。 stringSearch("欢迎来到 È", "wel È"); 'wel---- ---È' 和 stringSearch("google character קום iwijad", "google קום "); '谷歌 ----------קום '
      • 试试这个输入:“在这家公司,我们是 ?️‍? 包容性的!”这用于搜索模式:“h?v”。
      猜你喜欢
      • 2012-11-01
      • 2012-08-30
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-31
      相关资源
      最近更新 更多