簡體   English   中英

正則表達式在 >2 個空格或制表符處拆分

[英]Regex to split at >2 spaces or tabs

我正在嘗試創建一個匹配 2 個或更多空格或制表符的正則表達式,基本上是text.split(" ")text.split("\t")的組合。 我如何創建它?

我的嘗試:(但它不起作用)

text.split(new RegExp("[  |\t]"))

編輯:這在空格/制表符處拆分,但我需要在 2 個或更多空格處拆分..

text.split("\\s+");
\s{2,}

你可以這樣試試……! \s{2,} 表示 2 個或更多

我從這個替換帖子Regex 中得到了這個想法,用一個空格替換多個空格

演示:http: //jsbin.com/akubed/1/edit

我同意@Will 評論 - 也添加選項卡空間

\s{2,}|\t
String s="This      is      test";
    String [] as=s.split("\\t{2,}");
    for(int i=0;i<as.length;i++)
    System.out.println(as[i]);

這對我有用。

我會在下面建議我的功能。 它將每個單詞分開,獨立於任何數量的空格/制表符。

 const filterWords = (text) => { const wordList = text.split(" "); let trimmedWordList = []; for (let index = 0; index < wordList.length; index++) { trimmedWordList.push(wordList[index].trim()); } return trimmedWordList.filter(function (e) { return e !== ""; }); } console.log(filterWords(" This is a demo text"));

暫無
暫無

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

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