Abdul Basit
Abdul Basit

Reputation: 1

How to detect two spaces in middle of the string?

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);
}

Upvotes: 0

Views: 2983

Answers (7)

adiga
adiga

Reputation: 35202

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);
}

Upvotes: 4

joohong89
joohong89

Reputation: 1271

To properly address your problem, you slice will return a two character string. However, you are comparing 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 comparison 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 comparing 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 comparison 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);
}

Upvotes: 0

meistermuh
meistermuh

Reputation: 522

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');
}

Upvotes: 0

Elanis
Elanis

Reputation: 181

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.

Upvotes: 0

Kobe
Kobe

Reputation: 6446

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)

Upvotes: 2

PEPEGA
PEPEGA

Reputation: 2271

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

var str = "Hello world, welcome 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);

Upvotes: 0

CertainPerformance
CertainPerformance

Reputation: 370659

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');
}

Upvotes: 4

Related Questions