简体   繁体   English

在忽略链接和html标签的同时替换内容

[英]Replace content while ignoring links and html tags

I'm replacing the smiley shortcuts from my text editor with img tags, but the logic I use also sees parts of links and tags as smiley shortcuts (eg :/ in http:// , or :p in cursor:pointer in the style attribute) 我将文本编辑器中的笑脸快捷方式替换为img标签,但我使用的逻辑也将链接和标签的一部分视为笑脸快捷方式(例如,样式中的:: /http://cursor:pointer的 :p属性)

How can I ignore all the links and html tags while replacing smiley shortcuts with images? 在用图像替换笑脸快捷方式时,如何忽略所有链接和html标签?

for(var key in shortcuts){
    // Check if the editor html contains the looped shortcut
    if(content.toLowerCase().indexOf(key) != -1){
        // Escaping special characters to be able to use the shortcuts in regular expression
        var k = key.replace(/[<>*()?']/g, "\\$&");

        // Make shortcuts case insensitive
        var regex = new RegExp(k, "ig");

        //Replace shortcuts with img tags (smileys)
        tinymce.activeEditor.setContent(content.replace(regex,'<img src="images/transparent.png" class="smiley_icon '+ shortcuts[key] +'">'));
    }
}

Description 描述

I think the way I'd approach this is to match everything bad along with everything good. 我认为我要采用的方法是将所有不好的东西都匹配起来。 Then in the expression only put into the capture group the text glyphs I was interested in. Later in the programming logic I'd test each match to see if capture group 1 was populated, if so then the match .index will show where in the string that match occurred. 然后在表达式中仅将我感兴趣的文字字形放入捕获组。稍后在编程逻辑中,我将测试每个匹配项,以查看是否填充了捕获组1,如果是,则匹配项.index将显示在捕获组中的.index位置。发生匹配的字符串。

This expression will find all the :/ , :) , :p , tags and urls, but capture group 1 will only contain the :) , :/ , or :p which are not part of a tag or url. 该表达式将找到所有:/:) ,: :p ,标记和url,但是捕获组1仅包含:):/:p ,它们不是标记或url的一部分。

https?:\\/\\/[^\\s]*|<\\/?\\w+\\b(?=\\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\\s>]*|[^>])*>|(:\\)|:P|:\\/)

在此处输入图片说明

Expanded 扩展

  • https?:\\/\\/[^\\s]* matches a url in plain text https?:\\/\\/[^\\s]*与纯文本网址匹配
  • | or 要么
  • <\\/?\\w+\\b(?=\\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\\s>]*|[^>])*> matches any open or close html tags <\\/?\\w+\\b(?=\\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\\s>]*|[^>])*>匹配任何打开或关闭的html标签
  • | or 要么
  • (:\\)|:P|:\\/) capture group 1 will get the desired text glyphs (:\\)|:P|:\\/)捕获组1将获取所需的文字字形

Example: 例:

Live example: http://regexr.com?35cv9 (Hover over the blue matches to see each of of the capture groups and index) 实时示例: http ://regexr.com?35cv9(将鼠标悬停在蓝色的匹配项上可以查看每个捕获组和索引)

Sample Text 示范文本

<a href=http://i.like.kittens style="cursor:point"> :) I had a :/ great time :p </a> check out http://some.url.com

matches 火柴

[0] => Array
    (
        [0] => <a href=http://i.like.kittens style="cursor:point">
        [1] => :)
        [2] => :/
        [3] => :p
        [4] => </a>
        [5] => http://some.url.com
    )

[1] => Array
    (
        [0] => 
        [1] => :)
        [2] => :/
        [3] => :p
        [4] => 
        [5] => 
    )

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM