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

  • Home
  • SEARCH
  • 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 8922157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:45:43+00:00 2026-06-15T06:45:43+00:00

<script> var String = 1 Apple and 13 Oranges; var regex = /[^\d]/g; var

  • 0
<script>
var String = "1 Apple and 13 Oranges";
var regex = /[^\d]/g;
var regObj = new RegExp(regex);
document.write(String.replace(regObj,'')); 
</script>

And it works fine – return all the digits in the string.

However when I put quote marks around the regex like this:

var regex = "/[^\d]/g"; This doesn’t work.

How can I turn a string to a working regex in this case?

Thanks

  • 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-15T06:45:46+00:00Added an answer on June 15, 2026 at 6:45 am

    You can create regular expressions in two ways, using the regular expression literal notation, or RegExp constructor. It seems you have mixed up the two. 🙂

    Here is the literal way:

    var regex = /[^\d]/g;
    

    In this case you don’t have use quotes. / characters at the ends serve as the delimiters, and you specify the flags at the end.

    Here is how to use the RegExp constructor, in which you pass the pattern and flags (optional) as string. When you use strings you have to escape any special characters inside it using a ‘\’.

    Since the ‘\’ (backslash) is a special character, you have to escape the backslash using another backslash if you use double quotes.

    var regex = new RegExp("[^\\d]", "g");
    

    Hope this makes sense.

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

Sidebar

Related Questions

<script type='text/javascript'> var str=('apple',16),('orange',7),('banana',12); var pattern=/\('([^']+)',([0-9]+)\)/gi; var reg=new RegExp(pattern); var arr=str.match(reg); document.write(arr); </script> In
In Firefox's and Chrome's consoles, this works (alerts script content): var script = document.createElement(script);
This is my shortened script: var string1 = This is string 1; var string2
my script: var Url = 'http://readonline.egscans.com/Remnant/Chapter_036_[END]'; var response = UrlFetchApp.fetch(Url).getContentText(); I get this error
For this piece of script: var die = Math.floor(Math.random()*6 + 1); It's expected to
I made this Greasemonkey script: var maxpi = 250; var p1 = /html/body/div/div[2]/div/div[2]/table[2]/tbody/tr[1]/td[11]; var
I have this jQuery script var dataString = class_id=+class_id; $.ajax({ type: POST, url: page.php,
that's part of my script: var count = $(.sliderItem).length; if (count < lp +
I have an array that was populated by this script : var imgs =
To simplify my question, let's say I have the following script: var sPath =

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.