2010-03-18 27 views
1

一個字符串的特定部分我有一個包含這樣的事情可以在javascript

"Hello bla bla bla bla ok, once more bla können. // this is static: it doesn't change 

This is a text, this also a text. // this is dynamically added text it can change each time 

My name mysurename // this is also static text 
www.blabla.com 
" 

現在我有內容,我必須得到字符串和第三部分的第一部分的字符串, 我想能夠有3個部分,我想我必須使用split()之類的東西來分割它。

string1 = "Hello bla bla bla bla ok, once more bla können.; 

string2 = ""; // i have this part 

string3 ="My name mysurename"; 

如果有幫助的第一部分與"können. " 結束,以上// its a fictional URL

+1

Umm兩個代碼段中顯示的字符串不匹配。你也不會說這個文本來自哪裏。它是否在一個變量?或者是什麼。那些線條只是爲了插圖或文本總是格式化,像第一個textpiece和第二個textart之間的一個換行符,以及第二個和第三個文本片斷之間的兩個換行符 – jitter 2010-03-18 14:08:14

+3

我已經閱讀了三次,我仍然無法弄清楚它是什麼。 – Pointy 2010-03-18 14:11:12

+0

內容包含在一個聰明的模板中, – streetparade 2010-03-18 14:14:23

回答

2
myString.split("\n"); 

你會得到3個部分組成的數組。

2

網址我不知道我能正確地分析問題的第三部分結束,但它看起來像你可能會想要收集兩個靜態字符串之間的任何文本。如果這是正確的,那麼答案是:

First static string(.*?)Second static string 

在JavaScript:

match = subject.match(/First static string([\s\S]*?)Second static string/); 
if (match != null) { 
    text = match[1] // capturing group 1 
} else { 
    // Match attempt failed 
} 
+0

我需要將它拆分爲3部分 – streetparade 2010-03-18 14:23:24

+0

這三部分是什麼?抖動已經修改我的解決方案,以匹配靜態字符串(但它沒有多大意義,捕獲正則表達式的靜態部分)... – 2010-03-18 14:33:12

4
match = subject.match(/(First static string)([\s\S]*?)(Second static string)/); 
if (match != null) { 
    statictext1 = match[1]; // capturing group 1 
    dynamictext = match[2]; // capturing group 2 
    statictext2 = match[3]; // capturing group 3 
} else { 
    // Match attempt failed 
} 
+0

謝謝我需要拆分文本在3部分 – streetparade 2010-03-18 14:25:27