简体   繁体   English

检查文本中的任何单词是否与数组匹配

[英]Check if any word from text matches an array

I want to achieve the following: 我要实现以下目标:

1) Check every word from a textarea if it is a guitar chord. 1)检查文本区域中的每个单词是否是吉他和弦。

To do that, I created an array which contains all characters that guitar chords have (if a word matches the characters from this array it is a guitar chord): 为此,我创建了一个数组,其中包含吉他和弦具有的所有字符(如果单词与该数组中的字符匹配,则为吉他和弦):

var Regex = /^\s*(?:(A|B|C|D|E|F|G|A#|C#|D#|F#|G#|Ab|Bb|Db|Eb|Gb){1}(?:m|\+|\-|aug|dim|add|b|#|1|2|3|4|5|6|7|8|9|0|\/)*\s*)+$/;

2) If a word matches, I want it to become a link, pointing to an URL customized by the word itself. 2)如果一个单词匹配,我希望它成为一个链接,指向由单词本身定制的URL。

eg The following line 例如下面的行

Am   Bb  C# Dadd9
Song lyrics here

should turn into 应该变成

<a href="example.com/Am">Am</a>   <a href="example.com/Bb">Bb</a>  <a href="example.com/C#">C#</a> <a href="example.com/Dadd9">Dadd9</a>
Song lyrics here

And here's what I've com up to for step 2: 这是我对步骤2的建议:

var link = "http://www.example.com/";
      $.each(chord, function(index, value) {   //append link to chord
        $('.output').append('<a href="' + link + value + '">' + value + '</a> ');
      });

But I need to define "chord". 但是我需要定义“和弦”。 How can I do to check each word, then if it is a chord (matching "Regex" characters) append link? 如何检查每个单词,然后检查它是否为和弦(匹配“ Regex”字符)附加链接?

You can use String.prototype.replace() . 您可以使用String.prototype.replace()

For brevity, let's assume we want to target the Am , Bb and C# chords. 为简便起见,假设我们要针对AmBbC#和弦。

// create a map for the chords to the anchors
var chordsAnchorsMap = {
    "Am": "<a href='https://example.com/a-minor'>Am</a>",
    "Bb": "<a href='https://example.com/b-flat'>Bb</a>",
    "C#": "<a href='https://example.com/c-sharp'>C#</a>"
};

// regular expression for the chords
var regex = /(Am|Bb|C#)/g;

var string = "Am    Bb    C#    Bb   M    Bb";

// replace all instances of the chords with their mapped anchors
var result = string.replace(regex,
    function(capturedChord) {
        // if chord is mapped to an anchor, replace it with the appropriate anchor
        if (chordsAnchorsMap.hasOwnProperty(capturedChord)) {
            return chordsAnchorsMap[capturedChord];
        }
        // else, return the just chord itself (in other words - don't replace it)
        return capturedChord;
    }
);

Now result would contain: 现在result将包含:

<a href='https://example.com/a-minor'>Am</a>    <a href='https://example.com/b-flat'>Bb</a>    <a href='https://example.com/c-sharp'>C#</a>    <a href='https://example.com/b-flat'>Bb</a>   M    <a href='https://example.com/b-flat'>Bb</a>

Here is a distilled version of @GuyWaldman's approach. 这是@GuyWaldman方法的简化版本。

What this does is to generate the HTML elements dynamically instead of using an object. 这样做是为了动态生成HTML元素,而不是使用对象。

 var chordPattern = /(?:(A|B|C|D|E|F|G|A#|C#|D#|F#|G#|Ab|Bb|Db|Eb|Gb){1}(?:m|\\+|\\-|aug|dim|add|b|#|1|2|3|4|5|6|7|8|9|0|\\/)+)/g; var str = `Am Bb C# Dadd9 Song lyrics here`; var html = str.replace(chordPattern, function(value){ return "<a href='https://example.com/"+value+"'>"+value+"</a>"; }); document.write(html); 

You may need to adjust the regex because I'm not sure if it will match all possible chords or if it matches only chords. 您可能需要调整正则表达式,因为我不确定它是否会匹配所有可能的和弦,或者是否仅匹配和弦。 You will also have to find a way to deal with the special characters in the URLs 您还必须找到一种方法来处理URL中的特殊字符

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

相关问题 检查字符串中的单词是否与数组中的单词匹配,如果匹配则将其从字符串中删除 - Check if a word in a string matches a word in an array and if so remove it from the string 如何检查文本中的任何单词是否在数组中? - How to check if any word inside a text is in array? 检查字符串中是否有任何单词在数组中 - Check if any word in string is in an array 如何检查输入的单词是否等于数组中的任何字符串 - How to check if typed word is equal to any string from array 检查/查找URL中是否有数组单词 - Check / find in url if the there're any word of the array 有没有办法检查单击按钮的 textContent 是否与数组中的任何值匹配? - Is there a way to check if the textContent of a clicked button matches any of the values in my array? 如何检查对象的值是否与 Angular 中数组列表中的任何值匹配 - How to Check If a Value of Objects Matches Any Value in a List of Array in Angular 如何检查字符串是否与node.js中的任何一个正则表达式匹配? - How to check if a string matches any of an array of regexes in node.js? 检查值是否与 JS ES6 中数组中的任何其他值匹配 - Check if value matches any of the other values in array in JS ES6 检查javascrpit中给定字符串数组中的字符串是否匹配 - check string matches from the given array of string in javascrpit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM