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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:09:52+00:00 2026-05-27T08:09:52+00:00

I have a box where the user inputs a regex, and in Javascript I

  • 0

I have a box where the user inputs a regex, and in Javascript I take that value and have another string tested with it like so: (this is an abstraction of my real issue)

var regex = $('input').val();
regex.test('some string');

The only way I know to make sure to cast the regex to a safe Object type, is to use eval().

Is that the best way of casting it?

  • 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-05-27T08:09:53+00:00Added an answer on May 27, 2026 at 8:09 am

    Use the RegExp constructor to create a pattern.

    // The next line would escape special characters. Since you want to support
    //    manually created RegExps, the next line is commented:
    // regex = regexp.replace(/([[^$.|?*+(){}])/g, '\\$1')
    
    regex = new RegExp(regex);
    //                      ^ Optionally, add the flags as a second argument, eg:
    //regex=new RegExp(regex, 'i');   //Case-insensitive
    

    UPDATE
    You seem to misunderstand the usage of the RegExp constructor. The “slash-notation” is the “primitive” way to create a Regular expression. For comparsion, consider (new is optional):

    "123"             ===    new String(123)
    false             ===    new Boolean(1)
    // Because a RegExp is an object, the strict compare `===` method evaluates to
    //  false if the pattern is not the same object.
    // Example:   /\d/ == /\d/   evaluates to false
    //      To compare a regex pattern, use the `pattern` property
    /[a-z]/i.pattern   ===    (new RegExp("[a-z]", "i")).pattern
    

    The RegExp constructor takes two arguments, with the second one being optional:

    1. String Pattern (without trailing and ending slashes)
    2. String (optional) Flags A combination of:

      • i (ignore case)
      • g (global match)
      • m (multi-line (rarely used)).

    Examples (new is optional):

    Using constructor                 using slash-notation   # Notice:
    RegExp('[0-9]');                /[0-9]/                  # no slashes at RegExp
    RegExp('/path/to/file\.html$')  /path\/to\/file\.html$/  # the escaped \ 
    RegExp('i\'m', 'i')             /i'm/i                   # \' vs ', 'i' vs /i
    

    Implementing a “RegExp” form field using slash-notation

    var regex = $('input').val();  //Example: '/^[0-9]+$/i'
    // Using a RegEx to implement a Reg Exp, ironically..
    regex = regex.match(/^\/([\S\s]+)\/([gim]{0,3})$/);
    regex = regex || [, regex, ""];        // If the previous match is null,
                                         // treat the string as a slash-less RegEx
    regex = new RegExp(regex[1], regex[2]);
    regex.test('some string');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string value from a user input box. I have to figure
I have another Javascript regex conundrum... (apologies if this is a bit long-winded -
I have an html file that accepts user inputs then uses Javascript to calculate
I want to have a text box that the user can type in that
I have an alert box that displays updates processed in php to the user
I have an ASP.NET user control that contains a text box control and a
I have a popup search box that is shown when the user mouses over
I have a function that presents the user a combo-box. def select_interface(interfaces) list_box :items
In my android application I have a dialog box in which the user inputs
I have written a set of javascript functions that allow me to validate user

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.