简体   繁体   中英

Javascript Uncaught SyntaxError: Invalid regular expression: /ID:(.*)) on local/: Unmatched ')'

With the function below I am trying to get it to retrieve the number 1003567400 by looking between 2 string variables set as start and end.

Here is the data:

data = ('(ID: 1003567400) on local');

And here is the code:

var start = "ID:";
var end = ") on Local";
var testRE = data.match(start + "(.*)" + end);
console.log(testRE[1]);

The error is:

Uncaught SyntaxError: Invalid regular expression: /ID:(.*)) on local/: Unmatched ')' at String.match (native)

How can I fix this issue?

 data = ('(ID: 1003567400) on local'); console.log(data.match(/\\(ID: (.*)\\) on local/)); 

Here's what you need: https://regex101.com/r/qtckPB/1

Your regex will be: \\(ID: (.*)\\) on local

data.match(/\\(ID: (.*)\\) on local/); will return you value

var start =“(ID:”;而不是var start =“ ID:”;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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