2

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.

1

5 Answers 5

4

I'd suggest, possibly:

val.split(/\W/);

References:

Sign up to request clarification or add additional context in comments.

1 Comment

Careful about the digits and _ excluded in \W, though.
3

[\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...

2 Comments

Cover what? What do the various characters do?
\W = everything which is not a letter, a number, or the underscrore; then adding the _ and all digits from 0-9. This has the benefit of covering non ASCII letters such as é ü etc
1

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

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

Comments

0

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.

Comments

0

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]+/)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.