簡體   English   中英

將字符串拆分為JavaScript中的單詞,標點符號和空格數組

[英]Split a string into an array of words, punctuation and spaces in JavaScript

我有一個字符串,我想分成數組中包含的項目,如下例所示:

var text = "I like grumpy cats. Do you?"

// to result in:

var wordArray = ["I", " ", "like", " ", "grumpy", " ", "cats", ".", "  ", "Do", " ", "you", "?" ]

我嘗試了以下表達式(和類似的變種沒有成功

var wordArray = text.split(/(\S+|\W)/)
//this disregards spaces and doesn't separate punctuation from words

在Ruby中有一個正則表達式運算符(\\ b),它在任何單詞邊界處分割,保留空格和標點符號,但我找不到類似的Java腳本。 非常感謝你的幫助。

String#match方法與regex /\\w+|\\s+|[^\\s\\w]+/g

  1. \\w+ - 任何單詞匹配
  2. \\s+ - 用於空格
  3. [^\\s\\w]+ - 用於匹配除空白和單詞字符之外的任何內容的組合。

 var text = "I like grumpy cats. Do you?"; console.log( text.match(/\\w+|\\s+|[^\\s\\w]+/g) ) 

正則表達式在這里解釋


僅供參考:如果您只想匹配單個特殊字符,則可以使用\\W. 而不是[^\\s\\w]+

邊界\\b一詞應該可以正常工作。

"I like grumpy cats. Do you?".split(/\b/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ". ", "Do", " ", "you", "?"]

編輯

處理案件. ,我們也可以在[.\\s]上拆分它

"I like grumpy cats. Do you?".split(/(?=[.\s]|\b)/)
// ["I", " ", "like", " ", "grumpy", " ", "cats", ".", " ", "Do", " ", "you", "?"]
  • (?=[.\\s]正向前看,在之前分裂.或者\\s
var text = "I like grumpy cats. Do you?"
var arr = text.split(/\s|\b/);
alert(arr);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM