最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect two spaces in middle of the string? - Stack Overflow

programmeradmin3浏览0评论

i have a code that is working fine for the double spaces("  ") at the end of the string, but won't work for spaces in middle of the any string.

var userInput = prompt("In which city do you live?");
lengthUserInput = userInput.length;
correctName = true;

for (i = 0; i < lengthUserInput; i++) {
  if (userInput.slice(i, i + 2) === " ") {
    correctName = false;
  }
}

if (correctName === false) {
  alert("Double spaces are not allowed");
} else {
  alert(userInput);
}

i have a code that is working fine for the double spaces("  ") at the end of the string, but won't work for spaces in middle of the any string.

var userInput = prompt("In which city do you live?");
lengthUserInput = userInput.length;
correctName = true;

for (i = 0; i < lengthUserInput; i++) {
  if (userInput.slice(i, i + 2) === " ") {
    correctName = false;
  }
}

if (correctName === false) {
  alert("Double spaces are not allowed");
} else {
  alert(userInput);
}

Share Improve this question edited Jun 17, 2019 at 8:31 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Jun 17, 2019 at 8:30 Abdul BasitAbdul Basit 11 silver badge1 bronze badge 5
  • Generally just "".split(' ').filter(e=> e.length).join(' ') – Dellirium Commented Jun 17, 2019 at 8:33
  • Do you want to remove the spaces or do you just want to alert the user of a illegal action? You could use regex to detect 2 or more spaces.. Try using "\s{2,}". – Martin M Commented Jun 17, 2019 at 9:08
  • @Dellirium that wiould filter all spaces, not double spaces. – VLAZ Commented Jun 17, 2019 at 10:04
  • @VLAZ whilst I agree, within the context of the task, I do believe that is the idea – Dellirium Commented Jun 17, 2019 at 11:04
  • @Dellirium OP is asking specifically for two spaces, not all spaces. And it's also for detecting them, not cleaning them. – VLAZ Commented Jun 17, 2019 at 11:16
Add a ment  | 

7 Answers 7

Reset to default 4

Just check whether the string .includes two spaces:

var userInput = prompt("In which city do you live?");
if (userInput.includes('  ')) {
  console.log("Double spaces are not allowed");
} else {
  console.log('ok');
}

You can use the regex \s{2}. The test method returns true if the string has 2 consecutive spaces (\s) anywhere in the string. (This would also returns true if >2 consecutive spaces are present)

var userInput = prompt("In which city do you live?");
var incorrectName = /\s{2}/.test(userInput)

if (incorrectName) {
  alert("Double spaces are not allowed");
} else {
  alert(userInput);
}

You can use a regex test for 2 or more spaces:

 var userInput = prompt("In which city do you live?");
userInput.test(/ {2,}/g) ? alert('Multiple spaces are not permitted') : alert(userInput)   

<!-- begin snippet: js hide: false console: true babel: false -->

If it isn't a problem to do so, you could just use a replace on the string to remove the extra spaces for them:

var userInput = prompt("In which city do you live?");
userInput = userInput.replace(/ {2,}/g, ' ')
alert(userInput)

You can use includes to see if a astring contains something. Includes ref

var str = "Hello world, wele to the universe.";
var n = str.includes("world");

Same can be used with the spaces

var str = "fre  refhyt";
var n = str.includes("  ");
console.log(n);

I like to use replace function to resolve this problem:

var userInput = prompt("In which city do you live?");

if (userInput !== userInput.replace(/  /g, ' ')) {
  alert("Double spaces are not allowed");
} else {
  alert(userInput);
}

Explaination: It will replace double spaces by simple spaces, so if both string are same: there's not double spaces.

if the position of the two spaces is not relevant, i.e. also the beginning/end of the string should be tested, you could also use pure javascript:

if (userInput.indexOf('  ') >= 0) {
    alert('some alert message');
}

To properly address your problem, you slice will return a two character string. However, you are paring it to a string of one space character. Thus it will never work when double spaces are in the middle of string.

The reason this parison works with double space at the back of the string is because at the last character, the slice will only return a string of 1 space character.

See the below snippet if paring properly with 2 spaces.

var userInput = prompt("In which city do you live?");
lengthUserInput = userInput.length;
correctName = true;

for (i = 0; i < lengthUserInput; i++) {
  // Note: the parison of double space instead of one as shown 
  if (userInput.slice(i, i + 2) === "  ") {
    correctName = false;
  }
}

if (correctName === false) {
  alert("Double spaces are not allowed");
} else {
  alert(userInput);
}

发布评论

评论列表(0)

  1. 暂无评论