6

With JavaScript I would like to remove all characters from a string which are not numbers, letters and whitespace. So remove characters like '%#$&@* so something like:

Player's got to * play! #justsaying

would become:

Players got to play justsaying

How can I do this, I'm not sure about regex.

Holly
  • 7,462
  • 23
  • 86
  • 140
  • 3
    Maybe `[^0-9a-zA-Z ]`? with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace – chris85 Jan 13 '17 at 15:54

6 Answers6

9

As @chris85 said, you can use the regular expression [^0-9a-z-A-Z] to replace all the characters that are not letters, numbers, or whitespace.

Here is a function that will do what you want:

function clean(str) {
    return str.replace(/[^0-9a-z-A-Z ]/g, "").replace(/ +/, " ")
}

The second replace call is needed to remove the runs of extra whitespace that are made from removing a character that is between spaces.

BookOwl
  • 378
  • 3
  • 11
4

Use replace

string.replace(/[^A-Z\d\s]/gi, '')

Note the two flags at the end of the regex

g - stands for global, and means that every such instance of the regular expression will be found

i - stands for case insensitive. That means it will match both lower case and uppercase characters

Playing with your string, it returns this output

"Players got to  play justsaying"

To transform two or more whitespace characters into a single whitespace, you could hain another replace method to the existing ones.

string.replace(/[^A-Z\d\s]/gi, '').replace(/\s+/g, ' ')

The key here is the + character, which finds one or more.

It's probably possible to do it more efficiently, but I'm an amateur in Regex.

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • thanks, is there a way I could remove double white spaces, so a double white space would become a single white space? – Holly Jan 13 '17 at 16:02
  • @Holy same concept. use replace to replace double whitespace with whatever you want. Richard Hamilton gave you a great answer, and I think you would benefit greatly from learning how to use `string.replace` vs. asking people how to do it – Gillespie Jan 13 '17 at 16:04
  • Modified the original answer – Richard Hamilton Jan 13 '17 at 16:08
  • I'd use a space, not `\s`, this will convert all new lines to spaces as well. – chris85 Jan 13 '17 at 16:18
2

I also added sign ' to regex because you have word Player's, so here my code:

var str = "Player's got to * play! #justsaying";
var result = str.replace(/[^a-z\d\s']/gi, '');
cn007b
  • 16,596
  • 7
  • 59
  • 74
1

ORIGINAL TEXT

Player's got to * play! #justsaying

REGEXP

([^a-zA-Z0-9\s])

RESULT

Players got to  play justsaying

TEST IT

const regex = /([^a-zA-Z0-9\s])/gm;
const str = `Player's got to * play! #justsaying`;
const subst = ``;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log(result);

See: https://regex101.com/r/BYSDwz/4

0

Try with

var text = "Player's got to * play! #justsaying";
var result = text.replace(/[^A-Z\d\s]/gi,'');
console.log(result);

Read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

0

This is how you might go about matching the characters you are interested in removing...

[^\d\w\s]

NB: That you should use the global modifier

Tested here: https://regex101.com/r/yilfcn/1 Refer to this post regarding how to apply it: javascript regexp remove all special characters

Community
  • 1
  • 1
Nick Allan
  • 387
  • 4
  • 10