[英]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. 为简便起见,假设我们要针对Am , Bb和C#和弦。
// 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.