Check if String starts with a space in JavaScript

featured Image

In this post, we will see how to check whether the first character in a string is a space or not using Javascript.

To check if a string starts with a space or not we can use:

  1. RegExp test method,
  2. String match method with regExp, or
  3. String startsWith method

Let’s see the above-mentioned methods with examples.

Using RegExp.test() method

We can call the regex test method on the following expression ^s*$ and pass on the string to detect if the string starts with a white space or not in JavaScript.

Example

function startsWithSpace(str){
  return /^s/.test(str);
}

console.log(startsWithSpace(' hello')) // true
console.log(startsWithSpace(' hello ')) // true
console.log(startsWithSpace('hello')) //false

Here, we have called the test() method on the regular expression /^s/ to match the pattern against the given string.

The / / (forward slash) indicates the start and the end of the expression.

The ^ indicate the start of a string or line.

The s matches any whitespace characters in the string, like tab, space, newline, line feed, form feed, etc.

Using match() method on string with regex

The match() is used to match a string against a regular expression. If a match is found it returns the array with the matched result. And it returns null if no match is found.

Example:

function startsWithSpace(str){
  return str.match(new RegExp( /^s/)) !== null; 
}

console.log(startsWithSpace(' hello')) //true
console.log(startsWithSpace('hello')) //false

Here, we have used the match() method on a string and passed the regex as an argument to match against the given string.

/ and / indicate the start and end of the regular expression.

^ specifies the start of the string or a line

s tells to check for white spaces in the string.

Using startsWith() method

The startsWith() method checks whether a string starts with a specific character in a given string. If the searched character is found it returns true else it returns false.

function startsWithSpace(str){
  return str.startsWith(" ");
}

console.log(startsWithSpace(' hello')) //true
console.log(startsWithSpace(' hello ')) //true
console.log(startsWithSpace('hello'))// false
console.log(startsWithSpace('hello '))// false

Related Topics:

JavaScript – Detect if a string contains any space

How to check if String contains only spaces in JavaScript

Check if String starts with a space in JavaScript

Replace all occurrences of a string in JavaScript

Javascript – Convert array to String (with and without commas)

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *