[英]Get Specific part of a String in 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. "
结尾,第三部分以上面的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
}
我不确定我是否可以正确解析该问题,但是看起来您可能想要收集两个静态字符串之间的任何文本。 如果是的话,答案是:
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
}
myString.split("\n");
您将获得一个由3部分组成的数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.