【问题标题】:Check if any word from text matches an array检查文本中的任何单词是否与数组匹配
【发布时间】:2018-02-28 20:07:22
【问题描述】:

我想实现以下目标:

1) 检查文本区域中的每个单词是否是吉他和弦。

为此,我创建了一个数组,其中包含吉他和弦具有的所有字符(如果单词与该数组中的字符匹配,则它是吉他和弦):

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)如果一个词匹配,我希望它成为一个链接,指向一个由词本身自定义的URL。

例如下面一行

Am   Bb  C# Dadd9
Song lyrics here

应该变成

<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

以下是我为第 2 步准备的内容:

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

但我需要定义“和弦”。如何检查每个单词,然后如果它是和弦(匹配“正则表达式”字符)附加链接?

【问题讨论】:

    标签: javascript jquery arrays var guitar


    【解决方案1】:

    您可以使用String.prototype.replace()

    为简洁起见,假设我们要定位 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;
        }
    );
    

    现在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>
    

    【讨论】:

    • 嗨,非常感谢。实际上,它似乎对我不起作用。你有什么小提琴或代码笔可以看到它的实际效果吗?我用 $('pre').html() 替换了您的字符串 var,这是我的预格式化元素中的内容(包含整个选项卡)
    【解决方案2】:

    这是@GuyWaldman 方法的精炼版本。

    这样做是动态生成 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);

    您可能需要调整正则表达式,因为我不确定它是匹配所有可能的和弦还是只匹配和弦。 您还必须找到一种方法来处理 URL 中的特殊字符

    【讨论】:

    • C# 的链接无法正常工作,因为# 是为 URL 片段保留的。我想这就是@GuyWaldman 使用c-sharp 的原因。
    • @fubar 是的,我已经编辑了我的答案,提到 URL 可能存在一些问题。
    • 我想如果我将 C# 放入一些数据值而不是链接中,这会起作用,对吧?
    • @Titus 不幸的是,这段代码在一行中返回了所有文本,而我将我的代码放入了一个预先格式化的元素中以保留空格和距离。看看这里jsfiddle.net/7s1fLrqj你能调整一下吗?
    • @AlbertoMoneti 你忘了添加 jQuery 库。这似乎有效jsfiddle.net/gsnozqhd/1 我添加了 jQuery 并对正则表达式进行了轻微修改。
    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多