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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:56:21+00:00 2026-06-13T19:56:21+00:00

Javascript Regular Expression is working fine first time but not second time, working again

  • 0

Javascript Regular Expression is working fine first time but not second time, working again for 3rd time and not for 4th and so on 🙁

Script :

<script language="javascript" type="text/javascript">
    var reg = /[^\w]/gi;
    function checkNonWordChars() {
        var str = $("#TestTextbox").val();
        if (reg.test(str)) {
            alert('!!! Non-Word Char Exists !!!');
        }
        else {
            alert('input accepted');
        }
    }    
</script>

HTML :

<input type="text" id="TestTextbox"  />
<input type="button" value="Test" onclick="checkNonWordChars();" />

If I click on button once, it will fire an alert saying that “!!! Non-Word Char Exists !!!” but if I click it again, it will fire an alert saying “input accepted” 🙁

  • 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-13T19:56:22+00:00Added an answer on June 13, 2026 at 7:56 pm

    OPTION 1

    Use the constructor rather than the literal notation:

    var reg = new RegExp('[^\w]','gi');
    

    More about differences between the two here: https://developer.mozilla.org/en-US/docs/Core_JavaScript_1.5_Guide/Regular_Expressions?redirect=no

    OPTION 2

    Mark the end of the string with a $ sign:

    var reg = /[^\w$]/gi;
    

    OPTION 3

    If your regular expression uses the “g” flag, you can use the exec method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression’s lastIndex property
    (test will also advance the lastIndex property).

    Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp/exec

    Therefore, in your case the test will work only 1st, 3rd, 5th, … time.

    Only in Firefox 3 there is a flag y , when specified exec always starts from 0 not lastIndex but this is probably not useful in your case.

    You can remove the g flag.

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

Sidebar

Related Questions

Why isn't my regular expression finding my string? Regex.Replace( SCRIPT language=Javascript src=\\\r\ntype=text/javascript , ^language=Javascript$,
I'm having some trouble translating my working C# regular expression into JavaScript's regular expression
I'm using JavaScript to do some regular expression. Considering I'm working with well-formed source,
Are Python and JavaScript regular expression syntax identical? If not, then: What are the
I would like a JavaScript regular expression that will match time using the 24
Why isn't this regular expression working? A correct email address does not pass the
I am using regular expression first time, and I really amazed! well, new discovery
I read in Javascript: The Good Parts by Douglas Crockford that javascript regular expression
Possible Duplicate: Interesting test of Javascript RegExp Regular expression test can't decide between true
I'm really struggling with the Javascript version of Regular Expression matching, despite knowing how

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.