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 8568335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:05:32+00:00 2026-06-11T18:05:32+00:00

Using JavaScript I need to check a string for the first instance of a

  • 0

Using JavaScript I need to check a string for the first instance of a given pattern and return everything after, and including, the pattern. See example below for a typical string, pattern and desired result.

The main problem I am having is that the pattern will almost certainly contain special characters such as parentheses. I cannot alter the pattern manually to escape those special characters. Unless I am able to do that using replace first?

e.g. result = string.match(pattern.replace(special with escaped))

I’m not sure if anything like that is even possible. Regex always gives me a headache and I would appreciate any pointers in the right, or alternative, direction.

Background

I am trying to do some complex mixins for LESS. So I am restricted to single line JavaScript by escaping with the back-tick. See here for information regarding using JavaSCript in LESS. I was hoping to use the new variadic argument support to get multiple color-stops. e.g. .radial-gradient(@shape, @position, @colorStops...) However LESS only gives you the full range of arguments passed when you use @arguments. So I am hoping to use regex to use @arguments for the string and @colorStop for the pattern and return everything after, and including, the first color-stop. LESS returns the first color-stop variable when using @colorStop. The general concept was taken from here)

For Example:

String: circle 0% 50% rgba(96, 16, 48, 0) 9px #661133 10px rgba(96,
16, 48, 0) 11px

Pattern: rgba(96, 16, 48, 0) 9px

Result: rgba(96, 16, 48, 0) 9px #661133 10px rgba(96, 16, 48, 0) 11px

  • 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-11T18:05:33+00:00Added an answer on June 11, 2026 at 6:05 pm

    “I cannot alter the pattern manually to escape those special characters. Unless I am able to do that using .replace first?”

    Yes you can. Given a variable pattern that is a string containing your regex pattern, then:

    pattern = pattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
    

    …will escape all of the characters that have special meaning in a regex, and put the result back in the same variable. Of course that only works if every character in your pattern is to be taken as a literal character to match.

    “I need to check a string for the first instance of a given pattern and return everything after, and including, the pattern”

    I’d simply append .* to the end of the pattern, so then it will match the specified bit followed by all of the following characters. That is, if a regex /abc/ matches the first instance of "abc" then the regex /abc.*/ will match "abc" plus every character following. (In a regex, . matches any character, and * matches the previous bit zero or more times, by default doing a “greedy” match.)

    Putting this together:

    function matchToEnd(pattern, str) {
        var re = new RegExp(pattern.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + ".*"),
            result = str.match(re);
        if (result)
           return result[0];
        else {
           // didn't match, return default value of your choice, e.g.:
           return null;
        }
    }
    
    var result = matchToEnd("rgba(96, 16, 48, 0) 9px",
                            "rgba(96, 16, 48, 0) 9px #661133 10px rgba(96, 16, 48, 0) 11px");
    

    Demo: http://jsfiddle.net/JVdnz/

    Note though that going back to the assumption I had to make earlier that your regex pattern didn’t contain any characters that have special meaning in a regex, if that is the case then you don’t need regex to do this task, you can just use the .indexOf() method in combination with the .substr() method:

    var i = str.indexOf(pattern);  // find index of first instance of pattern
    if (i != -1)
       return str.substr(i);       // return from that index to end of string
    

    Here’s a demo similar to previous demo but with .indexOf() instead of regex: http://jsfiddle.net/JVdnz/1/

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

Sidebar

Related Questions

Using JavaScript, I need to check if a given string contains a sequence of
i need to trim a string to its first 100 characters using jquery/javascript. also
Here i need to check a button click using javascript i.e)if button A is
I need to check a certain selection of checkboxes using JavaScript. All these checkboxes
I need using javascript to check if the current user is fan of my
I need to check whether document element value is changed after particular operation using
Background Using JavaScript, I need to sort a large JSON object based on a
I need to validate my textbox using JavaScript, so far i made this :
I need to write to a text file using JavaScript. I have a machine
I need to add markup to some text using JavaScript regular expressions. In Python

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.