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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:21:00+00:00 2026-06-08T06:21:00+00:00

Is it possible to output the first character from a string (its index) that

  • 0

Is it possible to output the first character from a string (its index) that causes a mismatch with a regular expression? Is it possible with just using regular expression matching operations or something more complex must be employed?

For instance, in JavaScript, I may have a regular expression /^\d{3}\s\d{2}$/ that matches string with 3 digits followed by a whitespace and another 2 digits. I have a string "123a45" to which I apply this regular expression. Doing this (e.g., "123a45".match(/^\d{3}\s\d{2}$/)) returns null since the regular expression is not matched. How can I get the first character that causes this mismatch (in this case "a", the character with the index 3)?

One use case for this could be to point user directly to the character that causes a string entered by the user to be invalid according to some regular expression used for its validation.

  • 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-08T06:21:02+00:00Added an answer on June 8, 2026 at 6:21 am

    You would need to break-down the regex pattern to all possible matching patterns for partial matches and such list of patterns ordered from the longest match to the shortest one (or none). Once you got match, calculating the lenght of (partial) match you’ll get position of the character that causes mismatch. Substring from that position with length of one character is exactly character that is behind this mismatch (if some). If there is no mismatch, then it returns empty (sub-)string.

    var s = "123a45";
    alert(s.substr(s.match(/^(\d{3}\s\d{2}|\d{3}\s\d|\d{3}\s|\d{0,3})/)[1].length,1));
    

    http://jsfiddle.net/ETWWS/

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

Sidebar

Related Questions

Is it possible to output specific JSON data (exported from Firefox bookmarks) using PHP.
Possible Duplicate: PHP String in Array Only Returns First Character I've got following problem.
I'd just like to know, is it possible to output the processing of a
Is it possible to format output with write.table? I can left-align columns using tab,
Possible Duplicate: eliminate unwanted output using awk and sed The following contents are there
Possible Duplicate: Running a command from Ruby displaying and capturing the output I have
Possible Duplicate: Form that removes WWW. and prints result on input? I'm using this
Is it possible to output only the first instance of a value stored in
Possible Duplicate: Shorten String in PHP (full words only) Below substr which iam using
Is it possible to get the (first?) match of a regex and output it

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.