Javascript Regex partial matching

I'm looking for a simple javascript regex that should match strings like

AAAA(abcd,6) AAAA(WXYZ,2)

but should not match strings like


I've come up withe the regex


but it matches all three of above.

Any help is much appreciated!

Thanks in advance!

-------------Problems Reply------------

That's because .* will match anything including ,6 Replace . with [^,] (any char but comma)


Depending on exactly what you want to match you could use something like


  • A{4} matches the character A exactly 4 times.
  • \( matches the character (
  • [a-zA-Z]{4} matches any lower or upper case character from a to z exactly 4 times.
  • , matches the character ,
  • \d matches a digit.
  • \) matches the character )

You could of course modify it to suit your needs, I recommend testing for instance at regex101 since it gives you instant feedback when you enter a regular expression.

var regex = /AAAA\([a-z]*,\d\)/i;

regex.test("AAAA(abcd,6)") => true;

regex.test("AAAA(WXYZ,2)") => true;

regex.test("AAAA(abcd,6,9)") => false;

Category:javascript Views:4 Time:2019-01-02

Related post

  • Javascript RegEx partial match 2012-01-30

    I have a regular expression pattern, which validates for a three digit number /^\d{3}$/.test("123") // true /^\d{3}$/.test("123.") // false I want to use this regex as an input restriction on a textbox. Basically, if the new value matches, i allow th

  • JavaScript RegEx Elements Match 2008-09-18

    I'm trying to match elements with a name that is 'container1$container2$chkChecked', using a regex of '.+\$chkChecked', but I'm not getting the matches I expect when the element name is as described. What am I doing wrong? --------------Solutions----

  • Javascript regex to match a person's height 2008-11-14

    I need a javascript regex pattern to match a person's height to check if the input is valid. Here are some sample input: 5' 9" 6' 5'8" Any ideas? --------------Solutions------------- If you want to make sure that no one mucks around with it, you coul

  • Javascript regex to match a string that ends with some characters but not with a particular combination of those 2010-04-08

    Let's say using Javascript, I want to match a string that ends with [abcde]* but not with abc. So the regex should match xxxa, xxxbc, xxxabd but not xxxabc. I am utterly confused. Edit: I have to use regex for some reason, i cannot do something if (s

  • Javascript Regex for matching whole words 2011-04-26

    This is a follow-up question to this one Since javascript regex is much different from .net regex (which I'm used to), I can't seem to figure out how to enhance this regex. Here's the current pattern: var pattern = new RegExp('\\b' + filter[i] + '\\b

  • How do you write a Javascript Regex that matches if file extension is not within (exe|js|htaccess) 2011-07-19

    I'm trying to write a javascript regexp for a blacklist of file extensions. I'm using a jquery plugin that has an option for acceptable file types that takes a regex, but rather than maintain a whitelist we would like to maintain a blacklist. So I ne

  • Javascript regex to match 'int' 2011-05-19

    I need a regex to match INT or INT, or INT , to split a String, the String could also contain similar words like INTEGER, INT444, INTEGER2, etc. but they should be ignored. var str = 'INTEGER INT, INTEGER2 INT , TEST INT'; str = str.split(/INT[\,\)\s

  • javascript regex to match characters between two strings 2009-09-09

    I am entirely new to regex, and am trying to use it to match vales in order to map them to variables (javascript looking at the output in responceText generated from a php script). At the moment I have this code: if (xmlhttp.readyState==4) { document

  • JavaScript Regex to match a URL in a field of text 2011-11-18

    How can I setup my regex to test to see if a URL is contained in a block of text in javascript. I cant quite figure out the pattern to use to accomplish this var urlpattern = new RegExp( "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&a

  • javascript RegEx hashtag matching #foo and #foo-fåäö but not 2010-04-06

    Currently we're using javascript new RegExp('#[^,#=!\s][^,#=!\s]*') (see [1]) and it mostly works, except that it also matches URLs with anchors like and also we'd rather avoid matching foo#bar Some attempts have been made w

  • Javascript regex for matching/extracting file extension 2011-07-05

    The following regex var patt1=/[0-9a-z]+$/i; extracts the file extension of strings such as filename-jpg filename#gif filename.png How to modify this regular expression to only return an extension when string really is a filename with one dot as sepa

  • Javascript regex not matching last char in form 2012-02-21

    I have this simple regex to catch the last instance of 'turn to 123' in a form (I have another regex for the main body): currenttext=currenttext.replace(/([^>])(turn\s+to\s+)(\d+)$/i,"$1<tt ref=\"$3\">$2$3</tt>"); which for example sub

  • Javascript regex get matches as array 2013-10-05

    I have the following code snippet: hash = window.location.hash; regex = /^#id=\d{1,10}&session=(.+)$/ig; if (regex.test(hash)) { stepTo4(0, id, "start", session); break; } How do I go about getting the matched value for \d{1,10} in the place wher

  • Javascript regex to match html comments, excluding those inside tags 2011-06-29

    I need to get html comments ( <!-- --> ) except for those inside tags, which are used sometimes. <style type="text/css"> <!-- .test { font-size: 12px; } --> </style> Getting html comments is quite easy with this regex: /<!-

  • Javascript regex that matches a decimal that is polluted with other characters 2011-10-02

    I'm trying to match the first set of digits in the following examples. some stuff (6 out of 10 as a rating) needs to return 6 some stuff (2.3 out of 10 as a rating) needs to return 2.3 some stuff (10 out of 10 as a rating) needs to return 10 Also, so

  • Javascript regex to match string but only show text 2011-10-25

    I have an HTML element with a title inside, like this. <details>Name of page</details> How can I make a regex to search for the <details> element, but only returning the text inside, Name of page? --------------Solutions------------

  • Javascript RegEx match problem 2010-03-04

    I have a sentence structure along the lines of [word1]{word2} is going to the [word3]{word4} I'm trying to use a javascript regex to match the words for replacement later. To do this, I'm working towards getting the following multi-dimensional array:

  • JavaScript Regex, match P but not PARAM 2009-09-08

    Using JavaScript RegEx. How can I match a <p> element(including attributes), but not <param> or other HTML elements starting with a "P". --------------Solutions------------- Try: /(<p(?:\s+[^>]*)?>)/i / ( #start capture group

  • Can java.util.regex.Pattern do partial matches? 2010-03-26

    Is it possible to know if a stream/string contains an input that could match a regular expression. For example String input="AA"; Pattern pat=Pattern.compile("AAAAAB"); Matcher matcher=pat.matcher(input); //<-- something here returning true ? or S

  • Regex - find and replace complete string occurrences only (not partial matches) 2010-04-02

    I'm not very good at regex but maybe there's a simple way to achieve this task. I'm given a string like "bla @a bla @a1 bla" I'm also given pairs like {"a", "a2"} , {"a1", "a13"}, and I need to replace @a with @a2 for the first pair, and @a1 with @a1

  • Javascript Regex multiple group matches in pattern 2010-05-18

    I have a question for a Javascript regex ninja: How could I simplify my variable creation from a string using regex grouping? I currently have it working without using any grouping, but I would love to see a better way! The string is: var url = 'reso

  • Match d-M-Y with javascript regex how? 2010-05-25

    my date formatting in PHP is d-M-Y and I'm trying to match the dates with a javascript regex: s.match(new RegExp(/^(\d{1,2})(\-)(\w{3})(\-)(\d{4})$/)) To be used with the jQuery plugin, tablesorter. The problem is it's not working and I'm wondering w

  • Javascript Regex match text between tildes 2011-03-26

    I feel like an idiot because I've spent a long time trying to find a regex that will work. String: ~05276~^~0500~^~Turkey... The ... means that there can be an unlimited number of characters after. What I want is the first tilde delimited number with

  • Regex patterns in Apache RewriteCond and friends: full or partial match? 2011-04-07

    Apache documentation states that CondPattern is the condition pattern, a regular expression which is applied to the current instance of the TestString [...] CondPattern is a perl compatible regular expression with some additions [...] And gives such

  • Javascript Regex match no spaces 2011-11-09

    I am trying to match the following string in Javascript Regex PC123456 This is what I have: /^PC\d*/ This works for every instance minus one with a space after the "PC" which does work but it should fail. Example: PC 123456 That should fail. What do

Copyright (C), All Rights Reserved.

processed in 0.074 (s). 11 q(s)