Navigate back to the homepage

how to allow space between words in javascript using regex?

Infinitbility
JavaScript
September 16th, 2022 · 1 min read
how to allow space between words in javascript using regex?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To allow space between words, use this regex /^\w+( \w+)*$/ it will return true if value contain space between words and false if space have in the left or right side and it’s also return false double space between words.

Let’s see short example to use regex in javascript

1const regex = /^\w+( \w+)*$/;
2console.log(regex.test("hello 8943"))

Today, I’m going to show you How do I check value contain space between words in javascript, as above mentioned, I’m going to use the above-mentioned regex with test() method.

Let’s start today’s tutorial how do you allow space between words in javascript using regex?

Here, I will show allow space between words in javascript and typescript.

Javascript regex allow space between words example

Here, we will create common validate function where we will validate param have space between words or not.

1function validate(param){
2 const regex = /^\w+( \w+)*$/;
3 return regex.test(param);
4}
5
6// validate empty
7console.log(validate(""))
8// false
9
10// validate number
11console.log(validate(39.89))
12// false
13
14// validate string with both side space
15console.log(validate(" infinitbility "))
16// false
17
18// validate string with double space
19console.log(validate("infinitbility aguidehub"))
20// false
21
22// validate string space between two words
23console.log(validate("infinitbility aguidehub"))
24// true
25
26// validate string
27console.log(validate("infinitbility"))
28// true

Output

Typescript regex allow space between words example

Here, we will create common validate function where we will validate param have space between words or not.

1function validate(param: string){
2 const regex = /^\w+( \w+)*$/;
3 return regex.test(param);
4}
5
6// validate empty
7console.log(validate(""))
8// false
9
10// validate string with both side space
11console.log(validate(" infinitbility "))
12// false
13
14// validate string with double space
15console.log(validate("infinitbility aguidehub"))
16// false
17
18// validate string space between two words
19console.log(validate("infinitbility aguidehub"))
20// true
21
22// validate string
23console.log(validate("infinitbility"))
24// true

Output

I hope it helps you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to allow only alphanumeric and space in javascript using regex?

how to allow only alphanumeric and space in javascript using regex?

An example of javascript regex allow only alphanumeric and space with the step-by-step guide.

September 15th, 2022 · 1 min read
how to allow only numbers and dot in javascript using regex?

how to allow only numbers and dot in javascript using regex?

An example of javascript regex allow only numbers and dot with the step-by-step guide.

September 14th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]