简体   繁体   English

获取JavaScript中字符串的特定部分

[英]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(); 现在我已经有了内容,我必须获得字符串的第一部分和第三部分,我希望能够有3部分,我想我必须使用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 如果有帮助,则第一部分以"können. "结尾,第三部分以上面的URL结尾// 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: 在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部分组成的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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