DEV Community

Cover image for How to Detect Symbols & spaces With RegEx!

Posted on


How to Detect Symbols & spaces With RegEx!

Regex Is Awesome, I am learning regex.

A good Example is to make a simple symbol & space detector with RegEx!
You can use RegExp in javascript

Use \W in RegExp to detect symbols & spaces.
& use .match() in Javascript to search for the symbols.

A RegExp is can be written as a pattern: /\W/g

Example In Javascript
   var text = prompt("Enter a Name (symbols & spaces not allowed!)"); // Take user Input.
   if(text.match(/\W/g) == null){ // Check if there are spaces, symbols.
     alert("Good Name!!"); // If valid.
     alert("Bad Name!! Remove Those Spaces And Symbols!"); // If Invalid
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

moopet profile image
Ben Sinclair

use .match() in Javascript to search for the symbols.

Nitpick: if you want to search for an occurrence of something by regex in Javascript, you can use the search method. match is for retrieving matching patterns (possibly more than one). You can use match if you like, but it's not exactly the same thing.

11 Tips That Make You a Better Typescript Programmer


1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields


Read the whole post now!