Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8334893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:29:41+00:00 2026-06-09T03:29:41+00:00

var mainvalue=This is first sentence. This is 2nd sentence. This is 3rd sentence.; var

  • 0
var mainvalue="This is first sentence. This is 2nd sentence. This is 3rd sentence.";
var matchwith="This is first";

Now I want to obtain the text in variable ‘mainvalue’ that occurs after the value stored in variable matchwith…. It is assumed here that the value in ‘matchwith’ is present in mainvalue.

Another question that I have…What if the value in ‘matchwith’ is stored more than once in mainvalue?

i.e.
var mainvalue=”This is first sentence. This is 2nd. Repeat of This is first sentence. Yet another sentence”;

Now, I want to obtain the text in ‘mainvalue’ that occurs before the last occurence of ‘This is first sentence’.

Also, I want to obtain the text in ‘mainvalue’ that occurs after the first occurence of ‘This is first sentence’

How do I do this?

Finally, how do I determine of the value “This is first sentence” occurs at beginning of mainvalue variable or not? Again, how do I find out if this value occurs right at the end of mainvalue variable or not?

I prefer pure JS, but even use of libraries like Jquery is ok with me…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:29:43+00:00Added an answer on June 9, 2026 at 3:29 am

    First get the position of the substring in the haystack:

    var pos = mainvalue.indexOf(matchwith);
    

    Then you can get the stuff before it:

    var before = mainvalue.substr(0,pos);
    

    Or after:

    var after = mainvalue.substr(pos+matchwith.length);
    

    Simple. Now, on to duplicates.

    In the case you outlined, you can get the string after the first match, and then get the part of that string that is before the second match (since that one will become the first). More flexible, though, is this:

    var pieces = mainvalue.split(matchwith);
    

    Now you can get the index of the piece you like.

    To see if the string starts or ends with your search pattern, you could see if the “string before/after the match” is empty, respectively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

var gethtml = $(#topmenu > li.l89 a).text().split(' ')[1].replaceWith('Any World'); The text is not changing
var SalesforceOAuthPlugin = { /** * Obtain authentication credentials, calling 'authenticate' only if necessary.
var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?
I really like this: var value = maxValue > minValue ? minValue : maxValue;
Hello I have a Google Visualisation Line chart like this... <script type=text/javascript src=https://www.google.com/jsapi></script> <script
Hi i've been getting this error message for a while now and i'd like
Let's say I have an entity that looks like this: public class Album() {
var doubleMaxValueString = double.MaxValue.ToString(CultureInfo.InvariantCulture); and I want to convert it back to double, so
This below example was my attempt at doing this: var source = Observable.Sample( Observable.Range(1,
I have an array of images that I want to loop through infinitely ie.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.