admin管理员组

文章数量:1313801

I have a string containing something like this

"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 
"

Now I have content and I have to get the first part of the string and the third part, I want to be able to have 3 parts, I think I have to split it using something like split();

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

string2 = ""; // i have this part 

string3 ="My name mysurename";

If it helps the first part ends with "können. " and the third part ends with the url above // its a fictional URL

I have a string containing something like this

"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. 
"

Now I have content and I have to get the first part of the string and the third part, I want to be able to have 3 parts, I think I have to split it using something like split();

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

string2 = ""; // i have this part 

string3 ="My name mysurename";

If it helps the first part ends with "können. " and the third part ends with the url above // its a fictional URL

Share Improve this question edited Mar 18, 2010 at 14:29 nickf 547k198 gold badges658 silver badges727 bronze badges asked Mar 18, 2010 at 14:03 streetparadestreetparade 32.9k39 gold badges105 silver badges123 bronze badges 5
  • 1 Umm the strings shown in the two code pieces don't match. Also you don't say where this text es from. Is it in a variable? Or what. And are those linebreaks only for illustration or the text always formatted like this one linebreak between 1st textpiece and 2nd textart and two linebreaks bewteen 2nd and 3rd textpiece – jitter Commented Mar 18, 2010 at 14:08
  • 3 I've read this three times now and I still can't figure out what it's about. – Pointy Commented Mar 18, 2010 at 14:11
  • The content is included from a smarty template, – streetparade Commented Mar 18, 2010 at 14:14
  • I need to split them in 3 parts, string.split("können."); doesnt work – streetparade Commented Mar 18, 2010 at 14:24
  • jQuery is a DOM manipulation toolkit and AJAX library. jQuery has nothing to do with splitting strings. – nickf Commented Mar 18, 2010 at 14:30
Add a ment  | 

3 Answers 3

Reset to default 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
}
myString.split("\n");

You'll get an array of 3 parts.

I'm not sure I can parse the question correctly, but it looks like you might be wanting to collect any text between two static strings. If that's right, then the answer is:

First static string(.*?)Second static string

In 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
}

本文标签: regexGet Specific part of a String in JavascriptStack Overflow