Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 正则表达式匹配多个可能被另一个单词分隔的单词,给出可能的中间单词列表_Javascript_Regex_Typescript - Fatal编程技术网

Javascript 正则表达式匹配多个可能被另一个单词分隔的单词,给出可能的中间单词列表

Javascript 正则表达式匹配多个可能被另一个单词分隔的单词,给出可能的中间单词列表,javascript,regex,typescript,Javascript,Regex,Typescript,我想使用regex来创建许愿,许愿和许愿让匹配,停止字列表和文本: let stopword: string[]= ["of", "the", "a"]; let to_match : string = "make wish"; let text: string = "make wish wish make a wish wish wish make the a wish make"; 我只能使用此正则表达式匹配许愿: const regex = new RegExp(`(?:\\b)$to_

我想使用regex来创建
许愿
许愿
许愿
匹配
停止字
列表和
文本

let stopword: string[]= ["of", "the", "a"];
let to_match : string = "make wish";
let text: string = "make wish wish make a wish wish wish make the a wish make";
我只能使用此正则表达式匹配
许愿

const regex = new RegExp(`(?:\\b)$to_match(?:\\b)`, "gi"); 
我想知道是否有可能像这样做

let to_match_splitted: string[] = to_match.split(" ");
const regex = `(?:\\b)${to_match_splitted[0]}\s(${any(stopword)}?)+\s${to_match_splited[1]}(?:\\b)`;
any(stopword)
匹配的是stopword列表中的任何stopword


还有一个正则表达式,它可以在列表中的每个字符串之间使用一个或多个停止字来匹配拆分的
的长度。

您可以创建一个类似的正则表达式

/\bmake(?:\s+(?:of|the|a))*\s+wish\b/gi
详细信息

  • \b
    -单词边界
  • 制作一个单词
  • (?:\s+(?:of | the | a))*
    -0次或多次出现
    • \s+
      -1+空格
    • (?:of | the | a)
      -of
the
a
(您可能希望使用
an?
来匹配
an
  • \s+
    -1+空格
  • wish
    -一个单词
    wish
  • \b
    -单词边界
  • 在代码中,可以使用

    let stopword:string[]=[“of”,“the”,“a”];
    让我们配对:string=“许愿”;
    let text:string=“许个愿许个愿”;
    const regex=new RegExp(`\\b${to_match.split(/\s+/).join((?:\\s+(?:“+stopword.join(“|“+”))))*\\s+”}\\b`,gi”);
    
    console.log(text.match(regex))您可以创建一个类似正则表达式的

    /\bmake(?:\s+(?:of|the|a))*\s+wish\b/gi
    
    详细信息

    • \b
      -单词边界
    • 制作一个单词
    • (?:\s+(?:of | the | a))*
      -0次或多次出现
      • \s+
        -1+空格
      • (?:of | the | a)
        -of
    the
    a
    (您可能希望使用
    an?
    来匹配
    an
  • \s+
    -1+空格
  • wish
    -一个单词
    wish
  • \b
    -单词边界
  • 在代码中,可以使用

    let stopword:string[]=[“of”,“the”,“a”];
    让我们配对:string=“许愿”;
    let text:string=“许个愿许个愿”;
    const regex=new RegExp(`\\b${to_match.split(/\s+/).join((?:\\s+(?:“+stopword.join(“|“+”))))*\\s+”}\\b`,gi”);
    
    console.log(text.match(regex))这里的编程问题是什么?什么不起作用?我找不到如何在正则表达式中使用单词列表作为变量(如果可能的话),以便在不使用循环的情况下有效地匹配模式,我将编辑我的帖子以使其更清楚。我不知道您是否在问正则表达式语法部分本身,或者关于如何处理数组并从中创建所需的正则表达式格式。如果您只想允许其中一个停止字位于这两者之间,而且根本不允许停止字,那么您可能需要一个类似于
    make(of the | a |)的正则表达式。如果它们(make、wish和可选的stop单词)都被一个空格分隔,那么这将是匹配的。如果你还需要一些其他的东西,比如在空白处有更多的灵活性等等,那么你需要在你对实际需求的描述中更加具体。谢谢你的回复,我意识到这不清楚,所以我编辑了我的帖子。你在这里的编程问题是什么?什么不起作用?我找不到如何在正则表达式中使用单词列表作为变量(如果可能的话),以便在不使用循环的情况下有效地匹配模式,我将编辑我的帖子以使其更清楚。我不知道您是否在问正则表达式语法部分本身,或者关于如何处理数组并从中创建所需的正则表达式格式。如果您只想允许其中一个停止字位于这两者之间,而且根本不允许停止字,那么您可能需要一个类似于
    make(of the | a |)的正则表达式。如果它们(make、wish和可选的stop单词)都被一个空格分隔,那么这将是匹配的。如果你需要其他东西,更多的关于空白的灵活性,等等-那么你需要更具体地描述实际需求。谢谢你的回复,我意识到它不清楚,我编辑了我的帖子。非常感谢,这正是我想要的!非常感谢,这正是我想要的!