最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript Jquery how to split any character different from text - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Mar 24, 2012 at 16:37 fire1fire1 821 silver badge10 bronze badges 1
  • regular-expressions.info/reference.html – Felix Kling Commented Mar 24, 2012 at 17:36
Add a ment  | 

5 Answers 5

Reset to default 4

I'd suggest, possibly:

val.split(/\W/);

References:

  • RegExp, at the Mozilla Developer Network.

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

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

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

Try this:

var myString = "Hello, I'm a string that will lose all my non-letter characters (including 1, 2, and 3, and all mas... 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.

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

发布评论

评论列表(0)

  1. 暂无评论