簡體   English   中英

在JavaScript中將特殊字符轉換為字符串

[英]Convert special character to string in javascript

嗨,我有一個vbscript函數,它將特殊字符替換為字符串。 我正在將該函數替換為javascript,但顯示錯誤。 下面是功能

<script language="vbscript">
Function ReplaceSpecialChars(strValue)      
    if strValue <> "" then
        strValue=Replace(strValue,"\","\\")
        strValue=Replace(strValue,"[","\[")
        strValue=Replace(strValue,"]","\]")
        strValue=Replace(strValue,"*","\*")     
        strValue=Replace(strValue,"^","\^")
        strValue=Replace(strValue,"$","\$")
        strValue=Replace(strValue,".","\.")
        strValue=Replace(strValue,"|","\|")
        strValue=Replace(strValue,"?","\?")
        strValue=Replace(strValue,"+","\+")
        strValue=Replace(strValue,"(","\(")
        strValue=Replace(strValue,")","\)")
        strValue=Replace(strValue,"{","\{")
        strValue=Replace(strValue,"}","\}")
    end if  
     ReplaceSpecialChars=strValue
End Function

我將此函數轉換為javascript函數,如下所示:

var replaceSpecialChars = function (strValue) {
if (strValue !== "") {
    strValue = strValue.replace("\'", "\\''");
    //strValue = strValue.replace(/\\/g, "\\\\"); 
    strValue = strValue.replace("[", "\[");
    strValue = strValue.replace("]", "\]");
    strValue = strValue.replace("*", "\*");
    strValue = strValue.replace("^", "\^");
    strValue = strValue.replace("$", "\$");
    strValue = strValue.replace(".", "\.");
    strValue = strValue.replace("|", "\|");
    strValue = strValue.replace("?", "\?");
    strValue = strValue.replace("+", "\+");
    strValue = strValue.replace("(", "\(");
    strValue = strValue.replace(")", "\)");
    strValue = strValue.replace("{", "\{");
    strValue = strValue.replace("}", "\}");
}
return strValue;

};

以下行正在使用哪個:

var a = function(value) {
     var varC = "(?:^|\\s)(" + replaceSpecialChars(value) + ")(?=\\s|$)"
                    varRegExp = new RegExp(varC, "ig")
                    if (expression1.search(varRegExp) != -1) {
                        alert("Duplicate value not allowed in Record " + (i + 1));
                        return false;
                    };

但是它顯示錯誤。

編輯

您為replaceSpecialChars函數發布的代碼適用於每個字符的第一個字符(當您取消注釋將'\\'字符替換為'\\'的行時)。 我希望該錯誤是因為您的替換未替換所有實例,所以您的正則表達式還剩下一堆特殊字符。


你想要

String.prototype.replace()

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/replace

為了使其替換所有實例,您將需要帶有附加字符'g'的正則表達式(這些初始化是等效的,但是第二個是首選的)

var regex = new RegExp('pattern', 'g');
var betterRegex = /pattern/g;

'g'告訴它找到所有實例,否則它將首先停止。

但是,請注意第二次初始化...

var regex = /\/g;

將會引發錯誤,因為它不能找到結束的'/'符號,因為您告訴它轉義了它。 您想要的是:

var regex = /\\/g;

告訴它尋找字符“ \\”。 其他所有字符也會發生類似的情況,因此請確保在正則表達式中將它們全部轉義。

對於字符串,只需要像這樣對'\\'進行轉義; 其他字符不需要。

因此,您的函數將看起來像這樣...

function replaceSpecialChars(str) {
   var result;
   if (str !== "") {
      result = str.replace(/\\/g, '\\\\');
      result = str.replace(/\[/g, '\[');
      result = str.replace(/\]/g, '\]');
      result = str.replace(/\*/g, '\*');
      ...
   }
   return result; 
}

有關在RegExp中具有特殊含義的字符的完整列表以及它們的作用,請參閱... https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/RegExp (實際上,您必須從描述中找出特殊字符,但是您已經在正則表達式中列出了幾乎所有特殊字符)

暫無
暫無

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

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