[英]Javascript: Replace special character code 8222 fails
在德國,文本處理器在其智慧中喜歡將低引號放在單詞的開頭,將大引號放在單詞的末尾。 因此,您最終會得到一個帶有 unicode 8222 代碼的文本,並且 running.search(String.fromCharCode(8222)) 發現該事件很好,並且 String.fromCharCode(8222) 也很好地顯示了該字符。
但是,現在它出現了問題-使用 fromCharCode 查找字符,我想下面的代碼將用空格替換它:
cSub.replace(/\String.fromCharCode(8222)/g, " ")
但它沒有,這也不起作用:
cSub.replace(/String.fromCharCode(8222)/g, " ")
在這兩種情況下,字符串都原樣返回。 我接近編寫自己的替換例程,但這不應該是解決方案,我猜?
關於如何用空格替換 8222 個字符的任何建議?
非常感謝
坦率
你不能使用這樣的正則表達式文字。 嘗試像這樣創建RegExp
:
const re = RegExp(`[${String.fromCharCode(8222)}${String.fromCharCode(8221)}]`, "g"); console.log(`Your RegExp ${re}`); console.log(document.querySelector("div").textContent.replace(re, ";"));
<div>"Something „quoted”"</div>
您可以通過傳遞特定值來使用不帶正則表達式的replace
。
function start() { var cId = "w3review"; var cTxt = document.getElementById(cId).value; cSub = cTxt.replace(String.fromCharCode(8222), " "); var iPos = cTxt.search(String.fromCharCode(8222)); document.getElementById("txtout").value = "Result: Character found at position " + iPos + ", result from replace: " + cSub; }
<textarea id="w3review" rows="5" cols="40"> Source Value: kennen. „Neurons that fire </textarea> <button onclick="start()">Click me</button> <textarea id="txtout" rows="5" cols="40"> </textarea>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.