Regular expression for matching Titles (ex: Book title) - javascript

I am using jquery.validate.js plugin to validate a form and I want regex with match Titles(Books or Non-Books or any Title of products) but I failed to match and I wanted a regex which match following,
=> From 'A-Z' , 'a-z', whitespace, as well as tab space , special characters like ' ( ' , ' ) ' , - , _ , and 'coma' , dot , semicolon, ifen ,' : ', and all numbers
I used following regex for above:
/^[a-zA-Z0-9.'\-_\s]$/
/^[\d,\w,\s\;\:\()]$/
/^[^.-_#][A-Za-z0-9_ -.]+$/ - this is showing error when Title starts from upper case 'A'
and I referred following sites
http://regexpal.com/ // in this site i checked the above characters bot it showed error on validate
http://regexlib.com/DisplayPatterns.aspx?AspxAutoDetectCookieSupport=1
http://www.vogella.com/articles/JavaRegularExpressions/article.html
thanks in advance

This regex should match what you want /^[A-Za-z0-9\s\-_,\.;:()]+$/.
Special characters like . & - need escaping with a backslash. You also need a + or * at the end of the square braces to say 'one or more' or 'any number of' respectively.

I think this one you want DEMO
^\w++(?:[.,_:()\s-](?![.\s-])|\w++)*$
Description
^ # Start of string
\w++ # Match one or more alnum characters, possessively
(?: # Match either
[.,_:()\s-] # a "special" character
(?![.\s-]) # aserting that it's really single
| # or
\w++ # one or more alnum characters, possessively
)* # zero or more times
$ # End of string

Related

Regex (JavaScript): match feet and/or inches

I'm trying to match feet and inches but I can't manage to get "and/or" so if first half is correct it validates:
Code: (in javascript)
var pattern = "^(([0-9]{1,}\')?([0-9]{1,}\x22)?)+$";
function testing(input, pattern) {
var regex = new RegExp(pattern, "g");
console.log('Validate '+input+' against ' + pattern);
console.log(regex.test(input));
}
Valid tests should be:
1'
1'2"
2"
2 (assumes inches)
Not valid should be:
* anything else including empty
* 1'1'
But my regex matches the invalid 1'1'.
Remove the + at the end (which allows more than one instance of feet/inches right now) and check for an empty string or illegal entries like 1'2 using a separate negative lookahead assertion. I've also changed the regex so group 1 contains the feet and group 2 contains the inches (if matched):
^(?!$|.*\'[^\x22]+$)(?:([0-9]+)\')?(?:([0-9]+)\x22?)?$
Test it live on regex101.com.
Explanation:
^ # Start of string
(?! # Assert that the following can't match here:
$ # the end of string marker (excluding empty strings from match)
| # or
.*\' # any string that contains a '
[^\x22]+ # if anything follows that doesn't include a "
$ # until the end of the string (excluding invalid input like 1'2)
) # End of lookahead assertion
(?: # Start of non-capturing group:
([0-9]+) # Match an integer, capture it in group 1
\' # Match a ' (mandatory)
)? # Make the entire group optional
(?: # Start of non-capturing group:
([0-9]+) # Match an integer, capture it in group 2
\x22? # Match a " (optional)
)? # Make the entire group optional
$ # End of string
try this
var pattern = "^\d+(\'?(\d+\x22)?|\x22)$";
Not to resurrect the dead, but here was my best shot at detecting fractional feet and inches. It will find:
3'
3'-1" or 3' 1"
3'-1 1/2" or 3' 1 1/2"
3'-1/2", 3' 1/2", 3'-0 1/2", or 3'0 1/2"
1"
1/2"
The only catch is your flavor of regex has to support conditionals.
pattern = "(\d+')?(?:(?(1)(?: |\-))(\d{1,2})?(?:(?(2) )\d+\/\d+)?\x22)?"

Split spaces avoiding double-quoted JS strings : from 'a "b \\" c" d ' to ['a','"b \\" c"','d']

I am currently building a small text editor for a custom file format. I have a GUI, but I also implemented a small output console. What I want to achieve is to add a very basic input field to execute some commands and pass parameters.
A command would look like :
compile test.json output.bin -location "Paris, France" -author "Charles \"Demurgos\""
My problem is to get an array containing the space-separated arguments, but preserving the double quoted parts which might be a string generated by JSON.stringify containing escaped double-quotes inside.
To be clear, the expected array for the previous command is :
[
'compile',
'test.json',
'output.bin',
'-location',
'"Paris, France"',
'-author',
'"Charles \\"Demurgos\\""'
]
Then I can iterate over this array and apply a JSON.parse if indexOf('"') == 0 to get the final result :
[
'compile',
'test.json',
'output.bin',
'-location',
'Paris, France',
'-author',
'Charles "Demurgos"'
]
Thanks to this question : Split a string by commas but ignore commas within double-quotes using Javascript . I was able to get what I need if the arguments do NOT contain any double-quotes. Here is the regex i got :
/(".*?"|[^"\s]+)(?=\s*|\s*$)/g
But it exits the current parameter when it encounters a double-quote, even if it is escaped. How can I adapt this RegEx to take care about the escaped or not double quotes ? And what about edge cases if I prompt action "windowsDirectory\\" otherArg, here the backslash is already escaped so even if it's followed by a double quote, it should exit the argument.
This a problem I was trying to avoid as long as possible during previous projects, but I feel it's time for me to learn how to properly take under-account escape characters.
Here is a JS-Fiddle : http://jsfiddle.net/GwY8Y/1/
You can see that the beginning is well-parsed but the last arguments is split and bugs.
Thank you for any help.
This regex will give you the strings you need (see demo):
"(?:\\"|\\\\|[^"])*"|\S+
Use it like this:
your_array = subject.match(/"(?:\\"|\\\\|[^"])*"|\S+/g);
Explain Regex
" # '"'
(?: # group, but do not capture (0 or more times
# (matching the most amount possible)):
\\ # '\'
" # '"'
| # OR
\\\\ # two backslashes
| # OR
[^"] # any character except: '"'
)* # end of grouping
" # '"'
| # OR
\S+ # non-whitespace (all but \n, \r, \t, \f,
# and " ") (1 or more times (matching the
# most amount possible))

How do I combine these two regular expressions into one?

I'm writing a rudimentary lexer using regular expressions in JavaScript and I have two regular expressions (one for single quoted strings and one for double quoted strings) which I wish to combine into one. These are my two regular expressions (I added the ^ and $ characters for testing purposes):
var singleQuotedString = /^'(?:[^'\\]|\\'|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*'$/gi;
var doubleQuotedString = /^"(?:[^"\\]|\\"|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*"$/gi;
Now I tried to combine them into a single regular expression as follows:
var string = /^(["'])(?:[^\1\\]|\\\1|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*\1$/gi;
However when I test the input "Hello"World!" it returns true instead of false:
alert(string.test('"Hello"World!"')); //should return false as a double quoted string must escape double quote characters
I figured that the problem is in [^\1\\] which should match any character besides matching group \1 (which is either a single or a double quote - the delimiter of the string) and \\ (which is the backslash character).
The regular expression correctly filters out backslashes and matches the delimiters, but it doesn't filter out the delimiter within the string. Any help will be greatly appreciated. Note that I referred to Crockford's railroad diagrams to write the regular expressions.
You can't refer to a matched group inside a character class: (['"])[^\1\\]. Try something like this instead:
(['"])((?!\1|\\).|\\[bnfrt]|\\u[a-fA-F\d]{4}|\\\1)*\1
(you'll need to add some more escapes, but you get my drift...)
A quick explanation:
(['"]) # match a single or double quote and store it in group 1
( # start group 2
(?!\1|\\). # if group 1 or a backslash isn't ahead, match any non-line break char
| # OR
\\[bnfrt] # match an escape sequence
| # OR
\\u[a-fA-F\d]{4} # match a Unicode escape
| # OR
\\\1 # match an escaped quote
)* # close group 2 and repeat it zero or more times
\1 # match whatever group 1 matched
This should work too (raw regex).
If speed is a factor, this is the 'unrolled' method, said to be the fastest for this kind of thing.
(['"])(?:(?!\\|\1).)*(?:\\(?:[\/bfnrt]|u[0-9A-F]{4}|\1)(?:(?!\\|\1).)*)*/1
Expanded
(['"]) # Capture a quote
(?:
(?!\\|\1). # As many non-escape and non-quote chars as possible
)*
(?:
\\ # escape plus,
(?:
[\/bfnrt] # /,b,f,n,r,t or u[a-9A-f]{4} or captured quote
| u[0-9A-F]{4}
| \1
)
(?:
(?!\\|\1). # As many non-escape and non-quote chars as possible
)*
)*
/1 # Captured quote
Well, you can always just create a larger regex by just using the alternation operator on the smaller regexes
/(?:single-quoted-regex)|(?:double-quoted-regex)/
Or explicitly:
var string = /(?:^'(?:[^'\\]|\\'|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*'$)|(?:^"(?:[^"\\]|\\"|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*"$)/gi;
Finally, if you want to avoid the code duplication, you can build up this regex dynamically, using the new Regex constructor.
var quoted_string = function(delimiter){
return ('^' + delimiter + '(?:[^' + delimiter + '\\]|\\' + delimiter + '|\\\\|\\\/|\\b|\\f|\\n|\\r|\\t|\\u[0-9A-F]{4})*' + delimiter + '$').replace(/\\/g, '\\\\');
//in the general case you could consider using a regex excaping function to avoid backslash hell.
};
var string = new RegExp( '(?:' + quoted_string("'") + ')|(?:' + quoted_string('"') + ')' , 'gi' );

Need help with a regular expression in Javascript

The box should allow:
Uppercase and lowercase letters (case insensitive)
The digits 0 through 9
The characters, ! # $ % & ' * + - / = ? ^ _ ` { | } ~
The character "." provided that it is not the first or last character
Try
^(?!\.)(?!.*\.$)[\w.!#$%&'*+\/=?^`{|}~-]*$
Explanation:
^ # Anchor the match at the start of the string
(?!\.) # Assert that the first characters isn't a dot
(?!.*\.$) # Assert that the last characters isn't a dot
[\w.!#$%&'*+\/=?^`{|}~-]* # Match any number of allowed characters
$ # Anchor the match at the end of the string
Try something like this:
// the '.' is not included in this:
var temp = "\\w,!#$%&'*+/=?^`{|}~-";
var regex = new RegExp("^["+ temp + "]([." + temp + "]*[" + temp + "])?$");
// ^
// |
// +---- the '.' included here
Looking at your comments it's clear you don't know exactly what a character class does. You don't need to separate the characters with comma's. The character class:
[0-9,a-z]
matches a single (ascii) -digit or lower case letter OR a comma. Note that \w is a "short hand class" that equals [a-zA-Z0-9_]
More information on character classes can be found here:
http://www.regular-expressions.info/charclass.html
You can do something like:
^[a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~][a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~.]*[a-zA-Z0-9,!#$%&'*+-/=?^_`{|}~]$
Here's how I would do it:
/^[\w!#$%&'*+\/=?^`{|}~-]+(?:\.[\w!#$%&'*+\/=?^`{|}~-]+)*$/
The first part is required to match at least one non-dot character, but everything else is optional, allowing it to match a string with only one (non-dot) character. Whenever a dot is encountered, at least one non-dot character must follow, so it won't match a string that begins or ends with a dot.
It also won't match a string with two or more consecutive dots in it. You didn't specify that, but it's usually one of the requirements when people ask for patterns like this. If you want to permit consecutive dots, just change the \. to \.+.

/PM regex syntax for sending message in chat-room

I am working on a AJAX/PHP chatroom and am currently stuck on the regex to detect if a user has send a PM & then work out who it is too and what the message is.
If the user types something like
/pm PezCuckow Hi There you so awesome!
I would like to first test if my string matched that pattern then get 'PezCuckow' and 'Hi There you so awesome!' as strings to post to the PHP.
I have done some research on regex but really have no idea where to start with this one!
Can you help?
==Thanks to everyones help this is now solved!==
var reg = /^\/pm\s+(\w+)\s+(.*)$/i;
var to = "";
if(message.match(reg)) {
m = message.match(reg);
to = m[1];
message = m[2];
}
This regex parses a message:
^(?:\s*/(\w+)\s*(\w*)\s*)?((?:.|[\r\n])*)$
Explanation:
^ # start-of-string
(?: # start of non-capturing group
\s*/ # a "/", preceding whitespace allowed
(\w+) # match group 1: any word character, at least once (e.g. option)
\s+ # delimiting white space
(\w*) # match group 2: any word character (e.g. target user)
\s+ # delimiting white space
)? # make the whole thing optional
( # match group 3:
(?: # start of non-capturing group, either
. # any character (does not include newlines)
| # or
[\r\n] # newline charaters
)* # repeat as often as possible
) # end match group 3
In your case ("/pm PezCuckow Hi There you so awesome!"):
group 1: "pm"
group 2: "PezCuckow"
group 3: "Hi There you so awesome!"
in a more general case ("Hi There you so awesome!")
group 1: ""
group 2: ""
group 3: "Hi There you so awesome!"
Note that the forward slash needs to be escaped in JavaScript regex literals:
/foo\/bar/
but not in regex patterns in general.
Hows about this:
var reg = /^\/pm\s+(\w+)\s+(.*)$/i,
m = '/pm PezCuckow Hi There you so awesome!'.match(reg);
m[0]; // "PezCuckow"
m[1]; // "Hi There you so awesome!"
That matches "/pm" followed by whitespace " " (liberally accepting extra spaces), followed by the username \w+, followed by whitespace " " agin, then finally the message .* (which is basically everything to the end of the line).
Assuming that only word characters (no spaces, etc) are valid in the name field, this'll do what you want:
var re = /(\/\w+) (\w+) (.+)/;

Categories