简体   繁体   English

突出显示HTML页面或WordPress编辑器中的不间断空格

[英]Highlight Non-breaking spaces in HTML page or WordPress editor

While editing in WordPress, I sometimes use non-breaking spaces in headers so that words stay together. 在WordPress中进行编辑时,我有时会在标题中使用不间断的空格,以便单词保持在一起。 When I save, the non-breaking spaces are there, but they look like normal spaces, so I can't see them. 当我保存时,不间断的空间就在那里,但它们看起来像普通的空间,所以我看不到它们。 Also, WordPress creates non-breaking spaces when I type in the body of my post, which I have to remove somehow. 此外,当我输入帖子的正文时,WordPress会创建不间断的空格,我必须以某种方式删除它。

I thought it'd be easy to create a bookmarklet that uses jQuery to highlight non-breaking spaces in a web page or the editor. 我认为创建一个使用jQuery来突出显示网页或编辑器中不间断空格的书签是很容易的。 However, I'm no good with regular expressions, or maybe there's something else I'm doing wrong. 但是,我对正则表达式并不擅长,或者可能还有别的我做错了。 Here's the jQuery code: 这是jQuery代码:

    $('p').html($('p').html().replace(/ [\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]/g, '<span class="red">&nbsp;</span>'));

Here's a jFiddle: https://jsfiddle.net/y18e0c1w/ 这是一个jFiddle: https ://jsfiddle.net/y18e0c1w/

======== ========

Maraca helped me out here (see below). Maraca帮我出去了(见下文)。 I created the bookmarklet with his code, and added a white-space:nowrap to the span so that you can still see the highlight if it's at the end of a line. 我用他的代码创建了bookmarklet,并在span中添加了一个白色空间:nowrap,这样你仍然可以看到高亮显示它是否在一行的末尾。 Here it is: 这里是:

javascript:function%20escapeRegExp(e){return%20e.replace(/([.*+?^=!:${}()\]\[\/\\])/g,"\\$1")}function%20replaceAll(e){return%20e.string.replace(new%20RegExp(escapeRegExp(e.search),"g"),e.replace)}jQuery("body").html(replaceAll({string:jQuery("body").html(),search:"&nbsp;",replace:'<u%20style="background:#FF0;white-space:nowrap">%20</u>'}));

Remember, it relies on jQuery already being loaded on the page. 请记住,它依赖于已经在页面上加载的jQuery。 It doesn't play nice with the WordPress backend, but it works on the frontend and that's good for me right now. 它与WordPress后端不太搭配,但它适用于前端,这对我现在很好。 Hope someone else finds this useful too. 希望其他人发现这也很有用。

Got it: https://jsfiddle.net/y18e0c1w/2/ 知道了: https//jsfiddle.net/y18e0c1w/2/

function escapeRegExp(s) {
    return s.replace(/([.*+?^=!:${}()\]\[\/\\])/g, '\\$1');   
}

function replaceAll(p) {
 return p['string'].replace(new RegExp(escapeRegExp(p['search']), 'g'), p['replace']);   
}

$('p').html(
    replaceAll({
        string: $('p').html(),
        search: '&nbsp;',
        replace: '<span class="red"> </span>'
    })
);

The first two functions are just helper functions. 前两个函数只是辅助函数。 Then I replace &nbsp; 然后我更换&nbsp; by a span, that's it. 一个跨度,就是这样。

Note that I used a normal space in the span, because then there is no problem with repeated execution. 请注意,我在span中使用了一个普通空格,因为重复执行没有问题。 Otherwise you would wrap the &nbsp; 否则你会包裹&nbsp; with span tags each exection. 每个例子都有span标签。

The quick and dirty solution for the whole body: https://jsfiddle.net/y18e0c1w/7/ 整个身体快速而肮脏的解决方案: https//jsfiddle.net/y18e0c1w/7/

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

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