简体   繁体   中英

Get Specific part of a String in Javascript

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

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
}

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

您将获得一个由3部分组成的数组。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM