2013-04-05 43 views
3

我想要插入一個回車每2個字的正則表達式... 也格式的字符串是在一個數組(這是否有任何區別?) 問題是:2周前我聽說過正則表達式,到目前爲止,它讓我覺得有人在腦中點燃。 所以目前我想出了(承擔與我...)插入回車使用正則表達式和JavaScript每2個單詞返回

ticketName[i] = 'Lorem ipsum dolor sit amet consectetur adipisicing'; 
ticketName[i] = ticketName[i].replace(/(?:[a-zA-Z])* {2}/ig, ','); 

令人驚訝......這是行不通的!

任何人都可以幫助我嗎?

此外,當我完成了這一點,當句子超過一定數量的字符時,我將需要用'...'替換句子的結尾。如果你有什麼見解,因爲我可能會回來那一個也... 搜索該網站,只發現用回車替換句子的結尾。而且我猜測我的pb在於這個詞的定義。 多謝

+0

'{2}'匹配2個空格。 – 2013-04-05 15:47:26

+1

回車在哪裏返回正則表達式? – 2013-04-05 15:49:11

+0

@silentboy,好吧,在測試時我正在使用昏迷,因爲我不是100%確定如何做回車......我認爲這是\ r或\ n ...但是在測試時最好不要冒險 – HowTo 2013-04-05 15:57:45

回答

2

一飽眼福,你是。我只是想我會添加一個選項,並在同一時間回答你的第二個問題:

var mytest = "lorem ipsum dolor sit amet consectetur adipisicing"; 
var newtext = mytest.replace(/(\w+\W+\w+)\W+/ig,"$1\n"); 
alert(newtext); 

result: 
lorem ipsum 
dolor sit 
amet consectetur 
adipisicing 

注 - 硬道理(「adipisicing」)不匹配,所以沒有更換。它只是在那裏結束(並沒有尾隨\n)。

說明:

正則表達式:

( start a capture group comprising: 
\w+ one or more "word" characters 
\W+ one or more "not a word" characters 
\w+ one or more "word" characters 
)  end capture group 
\W followed by non-word 
/ig case insensitive, global (repeat as often as possible) 

,並替換爲:

$1 "The thing in the first capture group (everything matched in parentheses) 
\n followed by a newline 

第二個問題:

順便說一句 - 因爲你阿斯金關於「其他」問題摹 - 如何終止一個句子...一定數量的單詞後,你可以做到以下幾點:

var abbrev = mytest.replace(/((\w+\W+){5}).*$/,"$1..."); 
alert(abbrev); 

result: 
lorem ipsum dolor sit amet ... 

說明:

( start capture group 
( start second group (to be used for counting) 
\w+ one or more "word" characters 
\W+ one or more "non word" characters 
){5} match exactly five of these 
) end of capture group 
.*$ followed by any number of characters up to the end of the string 

更換
$1 the contents of the capture group (the first five words) 
... followed by three dots 

那裏 - 一個問題的兩個答案!

如果你想兩者都做,然後做了「縮寫一個長句子」,然後再切成短段。使用正則表達式計算多行文字有點困難。

0

讓我們首先什麼是錯在你的正則表達式:
- [a-zA-Z]你不需要爲你使用修改
指定A-Z - 作爲由馬特·鮑爾說{2}評論,你只是想在這裏匹配2個連續的空格,你需要用括號來重複整個組
- ','你說你想用回車替換(\ r),但是你插入一個逗號

現在,最好的辦法來解決這個(在我看來)是:

/\b[a-z]+\b/ 

Here爲\ B,我們將使用lazy operators
你可以這樣做:

replace(/(\b[a-z]+\b.*?\b[a-z]+\b)/, '$1\r') 
0

嘗試:

'Lorem ipsum dolor sit amet consectetur adipisicing' 
    .match(/([a-z]+\s){2,2}/gi) 
    .join('\n'); 
1

我認爲這應該工作:

str.replace(/((\s*\w+\s+){2})/g, "$1\n"); 

如:

var str = 'Lorem ipsum dolor sit amet consectetur adipisicing'; 
str.replace(/((\s*\w+\s+){2})/ig, "$1\n"); 
"Lorem ipsum 
dolor sit 
amet consectetur 
adipisicing" 

解釋的正則表達式:

(  -> start of capture group. We want to capture the two words that we match on 
(  -> start of an inner capture group. This is just so that we can consider a "word" as one unit. 
\s*\w+\s+ -> I'm consider a "word" as something that starts with zero or more spaces, contains one or more "word" characters and ends with one or more spaces. 
)   -> end of capture group that defines a "word" unit. 
{2}  -> we want two words 
)   -> end of capture group for entire match. 

在更換,我們有$1\n這只是說 「使用第一個捕獲組和追加一個換行符」。

Fiddle

1

我希望你找到這裏發佈的正則表達式解決方案之一適合你。

另外,使用字符串split()和substring()方法也可以完成相同的操作。下面的代碼是這樣做的和一個小提琴的here。儘管這不像使用正則表達式那麼優雅。

源代碼示例

ticketName = new Array(); 
ticketName[0] = 'Lorem ipsum dolor sit amet consectetur adipisicing'; 
tmparray = new Array(); 
tmparray = ticketName[0].split(" "); 



finalName = ""; // hold final name 
totallength=0; 
maxlength = 30; 

// add a carriage return (windows) every two words 
for (var i=0; i<tmparray.length; i++){ 
    finalName += tmparray[i]+" "; 
    if ((i>0) && (((i-1)%2) == 0)) 
     finalName +="\r\n"; 
} 

// truncate result if exceed final length and add .... where truncated 
if (finalName.length>maxlength) 
    finalName= finalName.substring(0,maxlength)+ "....."; 

alert(finalName); 
+0

好吧,在這一個我寧願使用正則表達式。但感謝雖然因爲我不知道這是可能的js。它可能會在其他用途​​上派上用場。 – HowTo 2013-04-05 17:29:25

相關問題