- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to remove non-word characters in JavaScript?
Removing non-word characters
To remove non-word characters we need to use regular expressions. The logic behind removing non-word characters is that just replace the non-word characters with nothing('').
Example
In the following example there are many non-word characters and in between them there exists a text named "Tutorix is the best e-learning platform". So using regular expressions the non-word characters were replaced with nothing('') so as to get the word characters as the output.
<html> <body> <script type="text/javascript"> function remNonWord (string) { if ((string===null) || (string==='')) return false; else string = string.toString(); var PATTERN = /[^\x20\x2D0-9A-Z\x5Fa-z\xC0-\xD6\xD8-\xF6\xF8-\xFF]/g; return string.replace(PATTERN, ''); } document.write(remNonWord('Tutorix is the ~!@^&";\'/?>#$%*()+`={}[]|\:<.,best e-learning platform')); </script> </body> </html>
Output
Tutorix is the best e-learning platform
- Related Articles
- JavaScript Remove non-duplicate characters from string
- How to remove non-alphanumeric characters in PHP stirng?
- How to remove non-ASCII characters from strings
- How to remove all non-alphanumeric characters from a string in MySQL?
- PHP program to remove non-alphanumeric characters from string
- Remove all non-alphabetical characters of a String in Java?
- Reverse word starting with particular characters - JavaScript
- How to match word characters using Java RegEx?
- Find non-word character in a string with JavaScript RegExp
- JavaScript - Remove first n characters from string
- Number of non-unique characters in a string in JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- How to match non-word boundaries using Java RegEx?
- How to remove unique characters within strings in R?
- How to Remove Characters from a String in Arduino?

Advertisements