DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,155 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How to Detect Symbols & spaces With RegEx!
MAFEE7
MAFEE7

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.
   }else{
     alert("Bad Name!! Remove Those Spaces And Symbols!"); // If Invalid
   }
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
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.

This post blew up on DEV in 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!