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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:20:24+00:00 2026-05-16T14:20:24+00:00

I have a regular expression to match usernames (which functions in PHP using preg_match

  • 0

I have a regular expression to match usernames (which functions in PHP using preg_match):

/[a-z]+(?(?=\-)[a-z]+|)\.[1-9][0-9]*/

This pattern matches usernames of the form abc.124, abc-abc.123, etc.

However, when I take this to JavaScript:

var re = new RegExp("/[a-z]+(?(?=\-)[a-z]+|)\.[1-9][0-9]*/"); 

I receive a syntax error:

SyntaxError: Invalid regular expression: /[a-z]+(?(?=-)[a-z]+|).[1-9][0-9]*/: Invalid group

The (?(?=\-)[a-z]+|) is to say if after [a-z]+ we see a - then assert that [a-z]+ is after it otherwise, match nothing. This all works great in PHP, but what am I missing about JavaScript that is different?

EDIT: I appreciate the comments, and now I have one last question regarding this:

    var str="accouts pending removal shen.1206";
    var patt= new RegExp("/[a-z]+(?:-[a-z]+)?\.[1-9][0-9]*/"); 
    var result=patt.exec(str);
    alert(result); 

This alert comes up as null? But if I do the following it works:

var patt=/[a-z]+(?:-[a-z]+)?\.[1-9][0-9]*/;
var result=patt.exec(str);
alert(result); 

Why does “new RegExp()” not work?

  • 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-16T14:20:24+00:00Added an answer on May 16, 2026 at 2:20 pm

    Different regular expression engines support different features. Conditionals are not supported by Javascript.

    In any event, the conditional is unnecessary for your pattern. I would simplify your expression to /[a-z]+(?:-[a-z]+)?\.[1-9][0-9]*/, which is easier to understand and will work in both PHP’s PCRE and in Javascript.

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

Sidebar

Related Questions

I have a regular expression to find :ABC:`hello` pattern. This is the code. format
I have a regular expression to match filenames which look like this: name -
re = //?(\w+)/(\w+)/ s = '/projects/new' s.match(re) I have this regular expression which I
I have the following regular expression with which I match a few lines. Here
I have this regular expression: ([http://some.url.com/index.php?showtopic=\]*)([0-9]+(?:\.[0-9]*)?) its for extracting links to topics from forum
I have a regular expression: l:([0-9]+) This should match this string and return three
Assuming I have this regular expression: '#artists/(.*)/#' and I want to match this string:
I have a python function which uses a regular expression to match one (the
I have this regular expression in my Javascript code: /##+[A-z]+##/ When using it on
I have a regular expression to match 2 different number formats: \=(?[0-9]+)\?|\+(?[0-9]+)\? This should

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.