javascript - Test String if each letter has '+' sign on both sides of it - Stack Overflow

this program is supposed to test str, and if every letter in str has a '+' sign on both sides

this program is supposed to test str, and if every letter in str has a '+' sign on both sides of it then the function should return true. Otherwise, it should return false. I keep getting the error "SyntaxError: invalid quantifier".

function SimpleSymbols(str) { 
  var boolean = false;
  for(var i=1;i<(str.length-1);i++){
    if(/\w/.test(str.charAt(i))){
      if(str.charAt(i-1).match('+') && str.charAt(i+1).match('+')){
        boolean = true;
      }else{
        boolean = false;
      }
    }
  }
  str = boolean;
  return str; 

}

this program is supposed to test str, and if every letter in str has a '+' sign on both sides of it then the function should return true. Otherwise, it should return false. I keep getting the error "SyntaxError: invalid quantifier".

function SimpleSymbols(str) { 
  var boolean = false;
  for(var i=1;i<(str.length-1);i++){
    if(/\w/.test(str.charAt(i))){
      if(str.charAt(i-1).match('+') && str.charAt(i+1).match('+')){
        boolean = true;
      }else{
        boolean = false;
      }
    }
  }
  str = boolean;
  return str; 

}
Share Improve this question asked Jan 26, 2014 at 2:23 Colin Michael FlahertyColin Michael Flaherty 7962 gold badges7 silver badges15 bronze badges 1
  • Answered a similar question in here, if you want there are some solutions codereview.stackexchange./questions/39356/… – elclanrs Commented Jan 26, 2014 at 2:25
Add a ment  | 

3 Answers 3

Reset to default 5

match is used for regular expressions, so it's trying to convert '+' to a regular expression, but it's failing because /+/ isn't a valid regular expression (it should be '\\+' or /\+/). But it's easier to just directly test each character, like this:

  if(str.charAt(i-1) == '+' && str.charAt(i+1) == '+'){

Also note that /\w/ matches any 'word' character, which includes letters, numbers, and underscores. To mach just letter characters use should use /[a-z]/i (the i at the end makes it case-insensitive, so it will also match upper-case letters).

But it seems a lot simpler to invert the condition. Just test to see if the string contains any letter not surrounded by + signs or a letter at the beginning or end of the string, and return false if it does, like this:

function SimpleSymbols(str) { 
    return ! /(^|[^+])[a-z]|[a-z]([^+]|$)/i.test(str);
}

Much easier:

function SimpleSymbols(str) {
    return !str.match(/[^+]\w/) && !str.match(/\w[^+]/);
}

The main problems with your function are:

  1. You don't test if the first and last characters are letters. It should be safe to run your for loop from index 0 to < str.length because even though this will result in a str.charAt(-1) and str.charAt(str.length) when testing for '+' these just return "" rather than an error. Or of course you could continue with testing from the second character through to the second last in the loop and add an additional test for the first and last characters.

  2. The .match() method does a regex match, so it tries to convert '+' to a regex and of course + has special meaning within a regex and doesn't match the literal. I'd suggest just using === '+' instead, though you could use .match(/\+/).

  3. You are returning whatever value the boolean variable ends up with, which means your function is ignoring the tests on all but the second-last character in the string. You should return false immediately if you find a letter that doesn't have '+' around it.

  4. Your question asked about "letters", but /\w/ doesn't test for a letter, it tests for letters or digits or underscores. If you actually want just letters use /[a-z]/i.

(Also there's no point assigning str = boolean, because JS function parameters are passed by value so this assignment won't affect anything outside the function.)

So:

function SimpleSymbols(str) { 
  for(var i=0;i<str.length;i++){
    if(/[a-z]/i.test(str.charAt(i))){
      if(str.charAt(i-1)!='+' || str.charAt(i+1) != '+'){
        return false;
      }
    }
  }
  return true;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744781019a4593340.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信