1

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

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

5
  • 1
    Umm the strings shown in the two code pieces don't match. Also you don't say where this text comes 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 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. Commented Mar 18, 2010 at 14:11
  • The content is included from a smarty template, Commented Mar 18, 2010 at 14:14
  • I need to split them in 3 parts, string.split("können."); doesnt work Commented Mar 18, 2010 at 14:24
  • jQuery is a DOM manipulation toolkit and AJAX library. jQuery has nothing to do with splitting strings. Commented Mar 18, 2010 at 14:30

3 Answers 3

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i need to split the text in 3 parts
2
myString.split("\n");

You'll get an array of 3 parts.

Comments

2

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
}

2 Comments

I need to split it in 3 parts
What three parts? jitter already modified my solution to also match the static strings (but it doesn't make much sense to capture static parts of a regex)...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.