Javascript,使用正则表达式仅替换HTML标记之外的内容 [英] Javascript, Use a regex to replace content outside of HTML tags only

查看:150
本文介绍了Javascript,使用正则表达式仅替换HTML标记之外的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript中编写正则表达式来替换HTML标记之外的字符串,并忽略HTML标记中的字符串。

I am trying to write a regular expression in JavaScript to replace strings that are outside of HTML tags, and to ignore the strings within HTML tags.

这是我的JavaScript代码:

Here's my JavaScript code:

var content = "Hi, my <span user="John">name</span> is &nbsp;John";
var user = 'John';
var regex = new RegExp('(&nbsp;)?' + user,'g');
content.replace(regex, function($0,$1){
    return $1 ? $0 : '<img src="images/user.png">';
});

我的正则表达式是(& nbsp;)?John

该模式按照我想要的方式工作,但它将匹配应用于标签数据,这是我不想要的。

The pattern works the way I want to, but it applies the matching to tag data, which I don't want.

因此,我们的想法是忽略标签之间的所有内容:< > ,并忽略:& nbsp; John

So, the idea is to ignore everything between tags: < and >, and to ignore: &nbsp;John.

可以这样做吗?

推荐答案

描述



此正则表达式将匹配 John 提供它在字符串的开头或结尾和/或两边都有空格。

Description

This regex will match John providing it is either at the start or end of the string and/or has white space on either side.

匹配John的正则表达式:(?:\ | |& nbsp; | ^)(约翰)(?= \ s | \ r | \ n | $)

Regex to match John: (?:\s|&nbsp;|^)(John)(?=\s|\r|\n|$)

此正则表达式包含最后一个正则表达式,并且还匹配所有html标记和纯文本URL。此处的订单很重要,因为 John 只会匹配提供它在html标记之外或不嵌入到URL中。

This regex incorporates that last regex and also matches all html tags and plain text urls. The order here is important because John will only match providing it's outside an html tag or not embeded into a URL.

正则表达式: https?:\ / \ / [^ \ s] * |< \ /?\\\ w + \ b(?= \ s |>)(? := '[^'] * '| = [^] *| = [^'] [^ \s>] * | [^>])*> | \&安培; NBSP;约翰|(约翰)

如果您使用最后一个正则表达式并将其传递给您的函数,那么只有 John s在标签之外& url将替换为字符串。

If you take this last regex and pass it through your function, then only Johns outside the tags & urls will be replaced with a string.

工作示例: http://repl.it/J4T

代码

var content = "<span name=\"John\" funnytag:John>John John &nbsp;John DoeJohn JohnDoe Mr.JohnDoe http://cool.guy.john/LikesKittens</span>";
var rePattern = /https?:\/\/[^\s]*|<\/?\w+\b(?=\s|>)(?:='[^']*'|="[^"]*"|=[^'"][^\s>]*|[^>])*>|\&nbsp;John|(John)/gi;

content.replace(rePattern, function(match, capture) {
    return capture ? "<img src=\"images/user.png\">" : match;
});

输出

< span name =Johnfunnytag:John>< img src =images / user.png> < img src =images / user.png> & nbsp; John Doe< img src =images / user.png> < img src =images / user.png> Doe先生< img src =images / user.png> Doe http://cool.guy.john/LikesKittens< / span>

这篇关于Javascript,使用正则表达式仅替换HTML标记之外的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆