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
- 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
3 Answers
Reset to default 4match = 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
版权声明:本文标题:regex - Get Specific part of a String in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741955300a2406955.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论