How can we search and save substring inside a string between two specified values dynamically. For example if a have the following set of strings.
var string1 = "This is.. my ..new string";
var string2 = "This is.. your ..new string";
Now what to do if i want to save the subsrting that is between two dots, "my" and "your" in this case, from the string, maybe in another variable or maybe by removing everything except "my". I know it can be possible using indexof("my") but that would not be dynamic.
Regular Expression is THE solution for this kind of problems. You can do some google on it. There's lots of documents and tutorials out there.
For your particular question, to get the string in between "..", you can use the following code
var match1 = string1.match('\\.\\.\\s*(.+?)\\s*\\.\\.');
match1 = match1 ? match1[1] : false;
var match2 = string2.match('\\.\\.\\s*(.+?)\\s*\\.\\.');
match2 = match2 ? match2[1] : false;
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加