簡體   English   中英

匹配撇號與正確的單引號

[英]Match apostrophe with right single quote

我在我的wordpress插件中構建自動鏈接功能,使用javascript和我有一個問題。

在輸出帖子內容時,wordpress將撇號“'”轉換為右單引號“'”。

例如,wordpress編輯器中的帖子內容如下所示:

<div class"post-content">This is content and don't skip this.</div>

在前端撇號被html實體替換為“'”:

This is content and don’t skip this.

然后我有javascript數組,其中包含應鏈接的關鍵字。 我的關鍵字包含撇號時遇到問題:

 "don't skip"

我有這個代碼:

var patern = new RegExp("\\bdon't skip\\b", "gi");

if (patern.test(node.data)) {
   link_keyword(node, element, link);
}

其中node.data是:

node.data = "This is content and don’t skip this."

正如您所看到的,在node.data中找不到關鍵字,因為在節點數據中撇號是正確的單引號。

我不想觸摸內容,所以我嘗試用右單引號“'”替換我的關鍵字中的撇號,但我的關鍵字看起來像這樣:

don�t skip

如果我用html實體替換撇號以獲得正確的單引號“'”,它將如下所示:

don&rsquo;t skip

所以在這兩種情況下,ti都不會匹配,也不會被替換。 所以有人知道如何在我的關鍵字有撇號時處理追逐,並且在帖子內容是使用正確的單引號代替撇號的相同關鍵字,如何找到匹配。

不要檢查右撇號,只需檢查這樣的字符

node.data = "This is content and don’t skip this.";    

var pattern = new RegExp("\\bdon\.*t skip\\b", "gi");

pattern.test(node.data);
=> true;

這樣你就不會觸及數據,只是在沒有撇號的情況下,但只是單詞它仍然可以工作。 如果你不希望它在沒有“'”時工作,那么只需刪除“\\”之后的“*”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM