语法错误:圆括号未结束,如何处理“()”

时间:2019-09-12 15:51:38

标签: javascript regex google-apps-script

我正在尝试清理用户输入的字符串(包含不想要的字符,变音符号等的任何字符),并仅替换为一个没有空格的字符串。

我遇到了此错误:

  

执行失败:SyntaxError:括号未结束。

它在以下行停止:class ObservableOpsSpec extends FlatSpec { val isEven: Int => Boolean = _ % 2 == 0 "Observable Ops" should "split a cold observable" in { val o = Observable(1, 2, 3, 4, 5) val o2 = o.publish val (l, r) = o2.split(isEven) val x= l.toListL.runToFuture val y = r.toListL.runToFuture o2.connect() x.futureValue shouldBe List(1, 3, 5) y.futureValue shouldBe List(2, 4) } "Observable Ops" should "split a hot observable" in { val o = PublishSubject[Int]() val (l, r) = o.split(isEven) val lbuf = l.toListL.runToFuture val rbuf = r.toListL.runToFuture Observable.fromIterable(1 to 5).mapEvalF(i => o.onNext(i)).subscribe() o.onComplete() lbuf.futureValue shouldBe List(1, 3, 5) rbuf.futureValue shouldBe List(2, 4) } }

当我们遇到几个重音字母(变音符号)时,我能够更新旧的旧代码。我看到了可以用来解决它的代码,但是以某种方式我无法弄清楚如何解决该问题。

idoff = accented.search(a2.charAt(i));

如果字符串像这样的function clean(a2) { /* if string contains accented letters, index below and use regular text */ var accented = 'ÁÀÂÃÄÄÅÆáàâãäåąÇçćčÐÉÉÊËèéêëðęÍÍÎÏíîïłÑñńÖÓŐÓÔÕØöóőôõøÜŰÙÚÛÜüűúûŠšŸÿýŽžŻżŁ'; var regularText = 'AAAAAAAAaaaaaaaaCcccDEEEEeeeeeeIIIIiiilNnnOOOOOOOooooooUUUUUUuuuuSSYyyZzZzL'; var idoff = -1,new_text = ''; var lentext = a2.toString().length -1 for (i = 0; i <= lentext; i++) { idoff = accented.search(a2.charAt(i)); if (idoff == -1) { new_text = new_text + a2.charAt(i); } else { new_text = new_text + regularText.charAt(idoff); } } // return new_text; /* Locate where in the string that contains ":", remove it including spaces and change string to lowercase */ var space = new_text.indexOf(":"); if (space > -1) { var answer = new_text.substring(space); answer = answer.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()"'+@<>?]/g,"") answer = answer.replace(/ /g,""); answer = answer.toLowerCase(); } else { var answer = new_text; answer = answer.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()"'+@<>?]/g,"") answer = answer.replace(/ /g,""); answer = answer.toLowerCase(); } return answer; } ,我想清理到这个ABC-XYZ-LMN (AB12): XxxX Set Çompanÿ

2 个答案:

答案 0 :(得分:3)

String.prototype.search需要一个正则表达式,但是您要传入一个字符(字符串)。当您遍历a2时,最终会遇到一个开放的括号(“ AB12”周围的括号)。圆括号不是有效的正则表达式。

要解决此问题,您可以改用String.prototype.includes

但是,我认为一个更优雅的解决方案可能看起来像这样:

function clean(a2) {
  /* if string contains accented letters, index below and use regular text */
  const accented = 'ÁÀÂÃÄÄÅÆáàâãäåąÇçćčÐÉÉÊËèéêëðęÍÍÎÏíîïłÑñńÖÓŐÓÔÕØöóőôõøÜŰÙÚÛÜüűúûŠšŸÿýŽžŻżŁ';
  const regularText = 'AAAAAAAAaaaaaaaCcccDEEEEeeeeeeIIIIiiilNnnOOOOOOOooooooUUUUUUuuuuSSYyyZzZzL'.split('');

  let answer = '';

  a2.split('').forEach((char) => {
    let accentIndex = accented.indexOf(char);

    if (accentIndex > -1) {
      answer += regularText[accentIndex];
    } else {
      answer += char;
    }
  });

  answer = answer.replace(/\W/gi, '');

  return answer.toLowerCase();
}

console.log(clean('ABC-XYZ-LMN (AB12): XxxX Set Çompanÿ'));

答案 1 :(得分:0)

在函数clean(a2){

之后添加代码
{
"name": "cubehelix-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
    "clean": "rm -fr build/index.html && rm -fr build/bundle.js && npm run clean-logs",
    "build": "webpack --progress --colors",
    "build-min": "webpack --config=webpack.min.config.js",
    "dev": "webpack-dev-server --devtool eval --progress --colors --content-base build --port 8090",
    "test-build": "mkdir -p lib && babel app --out-dir lib --source-maps",
    "test": "npm run test-build && mocha --require source-map-support/register --compilers js:babel-register"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
    "@babel/cli": "~7.6.0",
    "@babel/core": "~7.6.0",
    "@babel/preset-env": "~7.6.0",
    "@babel/preset-react": "^7.0.0",
    "@babel/register": "~7.6.0",
    "@babel/runtime": "~7.6.0",
    "babel-loader": "^8.0.6",
    "chai": "~4.2.0",
    "css-loader": "^0.23.1",
    "file-loader": "^0.9.0",
    "html-webpack-plugin": "~3.2.0",
    "mocha": "~6.2.0",
    "null-loader": "^0.1.1",
    "style-loader": "^0.13.0",
    "url-loader": "^0.5.7",
    "webpack": "~4.39.3",
    "webpack-cli": "^3.3.8",
    "webpack-dev-server": "~3.8.0"
},
"dependencies": {
    "chai": "~4.2.0",
    "classnames": "^2.2.5",
    "jquery": "~3.4.1",
    "lodash": "~4.17.15",
    "prop-types": "^15.7.2",
    "react": "~16.9.0",
    "react-custom-validators": "*",
    "react-dom": "~16.9.0",
    "react-timer-mixin": "~0.13.4"
}
}

...