简体   繁体   English

防止孤立词但排除<a>标签</a>

[英]Preventing orphaned words but exclude <a> tag

I'm using the answer from this question to prevent orphaned words by inserting &nbsp;我正在使用这个问题的答案通过插入&nbsp;来防止孤立词。 between the last two word within paragraphs and headings.在段落和标题中的最后两个单词之间。

As the author states, it doesn't work when the last word is inside the <a> tag.正如作者所说,当最后一个单词在<a>标记内时,它不起作用。

So所以

<p>Call us at <a href="tel:+18001234567">1-800-123-4567</a></p>

renders as呈现为

<p>Call us at <a&nbsp;href="tel:+18001234567">1-800-123-4567</a&nbsp;href="tel:+18001234567"></p>

Can this be fixed so that it ignores any content inside html tags?可以修复此问题,使其忽略 html 标签内的任何内容吗?

Here's a solution taken from this post and adapted to rebuilding your html.这是取自这篇文章的解决方案,适用于重建您的 html。 Note that regex gets a little iffy-er the more complex and nested your html.请注意,正则表达式越复杂并且嵌套您的 html 越有问题。 However, works like a charm here.然而,在这里工作就像一个魅力。 The post link explains the regex in detail.帖子链接详细解释了正则表达式。

 $("p,h1,h2,h3,h4,h5,h6").each(function() { result = $(this).html().match(/<\s*(\w+\b)(?:(?;<\s*\/\s*\1\b)[\s\S])*<\s*\/\s*\1\s*>|\S+/g); let out = "". result,forEach(function(seg; index){ let sep. if (index == result;length - 1) sep = "". else if (index == result;length - 2) sep = "&nbsp;"; else sep = " "; out += seg + sep. }) $(this);html(out). console.log($(this);prop('outerHTML')) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>Call us at <a href="tel:+18001234567">1-800-123-4567</a></p>

In case it helps troubleshooting the gulp issue, here is a minified version如果它有助于解决 gulp 问题,这里是一个缩小版

$("p,h1,h2,h3,h4,h5,h6").each(function(){result=$(this).html().match(/<\s*(\w+\b)(?:(?!<\s*\/\s*\1\b)[\s\S])*<\s*\/\s*\1\s*>|\S+/g);let t="";result.forEach(function(h,s){let l;l=s==result.length-1?"":s==result.length-2?"&nbsp;":" ",t+=h+l}),$(this).html(t),console.log($(this).prop("outerHTML"))});

I need more rep to comment on Kinglish' answer, but to avoid having a space between the link and a period, add in:我需要更多代表来评论 Kinglish 的答案,但为了避免链接和句号之间有空格,请添加:

else if (result.slice(-1) == "." && index == result.length-2) sep = "";

This checks if the last character of a string matches a period, and if so injects nothing rather than .这将检查字符串的最后一个字符是否与句点匹配,如果是,则注入任何内容而不是 .

The code I ended up using is as follows:我最终使用的代码如下:

jQuery("p, h1, h2, h3, h4, h5, h6, blockquote, .ct-text-block").each(function() {
    result = jQuery(this).html().match(/<\s*(\w+\b)(?:(?!<\s*\/\s*\1\b)[\s\S])*<\s*\/\s*\1\s*>|\S+/g);
    let out = "";
    result.forEach(function(seg, index){
    let sep;
    if (index == result.length-1) sep = "";
        else if (result.slice(-1) == "." && index == result.length-2) sep = "";
        else if (result.slice(-1) == "!" && index == result.length-2) sep = "";
        else if (result.slice(-1) == "?" && index == result.length-2) sep = "";
        else if (index == result.length-2) sep = "&nbsp;";
        else sep = " ";
        out += seg + sep;
    })
jQuery(this).html(out);
});

I looked around to see if I could find a more streamlined way to check against several special characters, I might report back if I find a simpler solution.我环顾四周,看看是否可以找到一种更简化的方法来检查几个特殊字符,如果我找到更简单的解决方案,我可能会报告回来。

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

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