如何识别正则表达式

这个问题在这里已经有了答案:

  • 是否有正则表达式来检测有效的正则表达式? 8个答案

  • 假设你想定位一个源(例如:一篇文章),并且想要检查哪个单词在该源中最常用:

    假设文章中的整个文本块都在一个字符串中,分配给变量“str”:

    // Will be used to track word counting
    const arrWords = [];
    // Target string
    const str = 'fsdf this is the article fsdf we are targeting';
    // We split each word in one array
    const arrStr = str.trim().split(' ');
    
    // Lets iterate over the words
    const iterate = arrStr.forEach(word => {
      // if a new word, lets track it by pushing to arrWords
      if (!arrWords.includes(word)) {
        arrWords.push({ word: word, count: 1 });
      } else {
        // if the word is being tracked, and we come across the same word, increase the property "count" by 1
        const indexOfTrackedWord = arrWords.indexOf(word);
        arrWords[indexOfTrackedWord].count++;
      }
    });
    
    // Once that forEach function is done, you now have an array of objects that look like this for each word: 
    arrWords: [
      {
        word: 'fsdf',
        count: 2
      },
      {
        word: 'this',
        count: 1
      },
      // etc etc for the other words in the string
    ];
    

    现在你可以通过console.log(arrWords)来查看结果!

    链接地址: http://www.djcxy.com/p/76735.html

    上一篇: How to identify a regular expression

    下一篇: regex for all legit regex's