fire1
fire1

Reputation: 82

JavaScript Jquery how to split any character different from text

i have this script: val.split( /(?:,.| )+/ )

and i need to split any character different from letter, like new line, white space, "tab" or dot... etc

I know you cannot write all characters, so give me one good example.

Upvotes: 2

Views: 2048

Answers (5)

gotofritz
gotofritz

Reputation: 3381

[\W_0-9] should cover them all.

\W: Everything which is not a letter, a number, or the underscore; then adding the _ and all digits from 0-9. This has the benefit of covering non ASCII letters such as é ü etc...

Upvotes: 3

David Thomas
David Thomas

Reputation: 253318

I'd suggest, possibly:

val.split(/\W/);

References:

Upvotes: 4

Tamara Wijsman
Tamara Wijsman

Reputation: 12348

I know you cannot write all characters, so give me one good example.

In character classes, you can enumerate characters with a -; like so: [a-zA-Z]

and i need to split any character different from letter, like new line, white space, "tab" or dot... etc

You can negate a group of characters like this: [^a-zA-Z]

I have this script: val.split( /(?:,.| )+/ )

Which can now be this script: val.split(/[^a-zA-Z]+/)

Upvotes: 0

Ry-
Ry-

Reputation: 224904

You can use [] to create a range of possible characters and prefix [^ to invert the range. So:

val.split(/[^a-z]+/i)

Upvotes: 1

Elliot Bonneville
Elliot Bonneville

Reputation: 53301

Try this:

var myString = "Hello, I'm a string that will lose all my non-letter characters (including 1, 2, and 3, and all commas... everything)"
myString.replace(/[^a-z\s]/gi, '').split(" ");

It'll split a string into an array, stripping out all non-letter characters as it goes.

Upvotes: 0

Related Questions