閱讀前,請注意我已經檢查了這些解決方案都無濟於事: JavaScript backslash (\) in variables is causing an error 和RegExp range out of order with complex pattern互動正則表達式科在Javascript中沒有處理複雜的正則表達式
後者是我有同樣的問題(消息話題從Chrome中調試器):
Uncaught SyntaxError: Invalid regular expression: /^[\\.-)(]*([0-9]{3})[\\.-)(]*([0-9]{3})[\\.-)(]*([0-9]{4})$/: Range out of order in character class
at new RegExp (<anonymous>)
我想在JavaScript中創建一個交互式的正則表達式部分但對於複雜的正則表達式,我得到上述錯誤。
這裏是我的javascript代碼:
var bodies = document.getElementsByTagName("body");
var body = bodies[0];
var maxLengthText = 500;
var numCol = 40;
var numRow = 10;
var testAreaDiv = document.createElement("div");
testAreaDiv.style.display = "table-row";
var regExArea = document.createElement("textArea");
regExArea.maxLength= maxLengthText;
regExArea.cols= numCol;
regExArea.rows = numRow;
regExArea.style.display = "table-cell";
regExArea.placeholder = "Enter regular expressions here";
var testTextArea = document.createElement("textArea");
testTextArea.maxLength = maxLengthText;
testTextArea.cols = numCol;
testTextArea.rows = numRow;
testTextArea.display = "table-cell";
testTextArea.placeholder = "Enter Text here";
var submitButton = document.createElement("button");
submitButton.innerHTML = "TestRegEx";
submitButton.type = "Submit";
var result = document.createElement("div");
var br = document.createElement("br");
submitButton.onclick = function(){
var exp = regExArea.value;
var expArr = exp.split("\\");
var newExp = "";
for(var $i=0; $i<expArr.length-1; $i++){
newExp += expArr[$i] + "\\\\";
}
newExp += expArr[expArr.length-1];
console.log(newExp);
var patt = new RegExp(newExp);
console.log(testTextArea.value);
var res = patt.exec(testTextArea.value);
result.innerHTML = "<p>" + res + "</p>";
}
testAreaDiv.appendChild(regExArea);
testAreaDiv.appendChild(testTextArea);
testAreaDiv.appendChild(br);
testAreaDiv.appendChild(submitButton);
testAreaDiv.appendChild(result);
body.appendChild(testAreaDiv);
在上面的代碼我認爲問題是,我並沒有逃脫我的反斜槓,但我不認爲這是一個問題,我認爲它是搞砸了我的引號,但我的RegExp構造函數接受一個字符串,所以我懷疑這是問題。其他人有任何解決方案嗎?我要嘗試把像這樣2個反斜線之間的輸入字符串:
newExp =/+ newExp + /;
但我只是想這一點絕望,我不認爲它會奏效。任何關於什麼可能是問題的反饋將真正讚賞。
這裏是我的HTML代碼,隨意複製/粘貼等:
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Place favicon.ico and apple-touch-icon.png in the root directory -->
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/4.2.0/normalize.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
<!--<link rel="stylesheet" href="css/main.css"> -->
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
<h1> Test Area</h1>
<script src="assets/js/testSection.js" type="text/javascript" charset="utf-8" async defer></script>
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-1.12.4.min.js"><\/script>')</script>
<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga=function(){ga.q.push(arguments)};ga.q=[];ga.l=+new Date;
ga('create','UA-XXXXX-Y','auto');ga('send','pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async defer></script>
</body>
</html>
任何反饋將幫助,謝謝。
前看到[*範圍不按順序在字符類在javascript *](HTTP:// stackoverflow.com/questions/17727884/range-out-of-order-in-character-class-in-javascript)。你沒有逃過'''裏面'[']''。或者它應該放置在角色類的開始/結束處。 –
@WiktorStribiżew是的,謝謝你,你和下面的評論指出了這個錯誤。 –