1

So a substring can take two parameters, the index to start at and the index to stop at like so

   var str="Hello beautiful world!";
   document.write(str.substring(3,7));

but is there a way to designate the start and stopping points as a set of characters to grab, so instead of the starting point being 3 I would want it to be "lo" and instead of the end point being 7 I would want it to be "wo" so I would be grabbing "lo beautiful wo". Is there a Javascript function that serves that purpose already?

0

5 Answers 5

5

Sounds like you want to use regular expressions and string.match() instead:

var str="Hello beautiful world!";
document.write(str.match(/lo.*wo/)[0]); // document.write("lo beautiful wo");

Note, match() returns an array of matches, which might be null if there is no match. So you should include a null check.

If you're not familiar with regexes, this is a pretty good source: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Sign up to request clarification or add additional context in comments.

2 Comments

Good answer - but be careful when you post links to w3school ;)
The downside to regex is that he will need to know which character are special in regex to escape them. This is quite important to know, esp. if the OP doesn't use regex before.
2

use the method indexOf:
document.write(str.substring(3,str.indexOf('wo')+2));

4 Comments

Actually, the answer is incomplete, since the OP wants to specify the starting string also.
@nhahtdh If you give someone a fish - he'll have his plate full for dinner, if you give him a net and teach him how to fish - he'll have his plate full for the rest of his life ;)
@alfasin: Well, you didn't teach him how to fish all the way, that's why I complain.
@nhahtdh on the contrary - I showed him an example of usage - now he should finish the rest, this way he'll remember it. If I will feed him with a spoon like a baby - tomorrow he won't remember a thing!
2

Yup, you can do this easily with regular expressions:

var substr = /lo.+wo/.exec( 'Hello beautiful world!' )[0];
console.log( substr ); //=> 'lo beautiful wo'

Comments

0

Use a regex brother:

   if (/(lo.+wo)/.test("Hello beautiful world!")) {
    document.write(RegExp.$1);
   }

You need a backup plan in case the string does not match. Hence the use of test.

3 Comments

Ugh! That "feature" has been deprecated for quite some time now for obvious security reasons... More info here.
wow, I did not know that, and I use js every day. How embarrassing.
I didn't know about it when I was using it till the variable got hijacked. lol
0

Regular expression may be able to achieve this to some extent, but there are many details that you must be aware of.

For example, if you want to find all the substrings that starts with "lo", and ends with the nearest "wo" after "lo". (If there are more than 1 match, the subsequent matches will pick up the first "lo" after the "wo" of last match).

"Hello beautiful world!".match(/lo.*?wo/g);

Using the RegExp constructor, you can make it more flexible (you can substitute "lo" and "wo" with the actual string you want to find):

"Hello beautiful world!".match(new RegExp("lo" + ".*?" + "wo", "g"));

Important: The downside of the RegExp approach above is that, you need to know what characters are special to escape them - otherwise, they will not match the actual substring you want to find.


It can also be achieve with indexOf, albeit a little bit dirty. For the first substring:

var startIndex = str.indexOf(startString);
var endIndex =  str.indexOf(endString, startIndex);
if (startIndex >= 0 && endIndex >= 0)
    str.substring(startIndex, endIndex + endString.length)

If you want to find the substring that starts with the first "lo" and ends with the last "wo" in the string, you can use indexOf and lastIndexOf to find it (with a small modification to the code above). RegExp can also do it, by changing .*? to .* in the two example above (there will be at most 1 match, so the "g" flag at the end is redundant).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.