Specifying complex conditions in regular expressions - javascript

The id attribute values in HTML 5 has the following rules
1.The string should contain nonwhitespace characters
2. It should contain at least one letter
How can i represent this in regular expression form.I reached in a regular expression which satisfies the first condition..
/(^|\s)\S+/ig
But how can i indicate the second condition in to the above regular expression
...and I am new to regular expressions...

You have got your restrictions wrong. The HTML5 ID data type must:
must be at least one character long
must not contain any space characters
That's:
^\S+$
Done.
Note:
Without any characters in your regex (\S is not a character), you don't have to make your regex case-insensitive (/.../i is superfluous).
Since your regex is anchored (^...$) there can only ever be a single match. This means there is no need for the "global" modifier, so /.../g is superfluous as well.

The easiest way would be to match on two expressions.
myString.match(/(^|\s)\S+/i) && myString.match(/[a-zA-Z]/)

Related

javascript regular expression allow name with one space and special Alphabets

how to write regular expression allow name with one space and special Alphabets?
I tried with this [a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)* but not working for me,
example string Björk Guðmundsdóttir
You may try something along these lines:
^(?!.*[ ].*[ ])[ A-Za-zÀ-ÖØ-öø-ÿ]+$
The first negative lookahead asserts that we do not find two spaces in the name. This implies that at most one space is present (or no spaces at all). Then, we match any number of alphabets, with most accented letters included. Spaces can also be matched, but the lookahead would already ensure that at most one space can be present.
Demo
Use this one:
[a-zA-Z\u00C0-\u00ff]*[ ]{1}[a-zA-Z\u00C0-\u00ff]*
Answer from other question

How do you match valid integers and roman numerals with a regular expression?

There is a very helpful article on how to validate roman numerals How do you match only valid roman numerals with a regular expression?
Regex has always been my achilles heel in spite of all my efforts. How do I expand the regular expression provided to match normal integers as well? The provided regular expression is:
/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/
Assuming that you just want decimal integers in regular form (e.g., 1000 would be valid, 1e3 [the same number in scientific notation] would not), the regular expression to validate that is \d+, which means "one or more digit(s)."
To have your regular expression allow that, you'd want an alternation, which lets either of two alternatives match. Alternations are in the form first|second where first and second are the alternatives.
Since your current expression has "beginning" and "end" of input assertions (^ and $), we'd either want to include those in the second alternative as well, or put the entire alternation in a non-capturing group.
So either:
/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$|^\d+$/
// Note -----------------------------------------------------^^^^^^
(on regex101)
or
/^(?:M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})|\d+)$/
// ^^^ ^^^^^
(on regex101)
Note that your original expression has a few capture groups (but doesn't entirely consist of captures); if you wanted to capture the \d+ part, you'd put () around it.

Matching variable-term equations

I am trying to develop a regular expression to match the following equations:
(Price+10%+100+200)
(Price+20%+200)
(Price+30%)
(Price+100)
(Price-10%-100-200)
(Price-20%-200)
(Price-30%)
(Price-100)
My regex so far is...
/([(])+([P])+([r])+([i])+([c])+([e])+([+]|[-]){1}([\d])+([+]|[-])?([\d])+([%])?([)])/g
..., but it only matches the following equations:
(Price+100+10%)
(Price+100+100)
(Price+200)
(Price-100-10%)
(Price-100-100)
(Price-200)
Can someone help me understand how to make my pattern match the full set of equations provided?
Note: Parentheses and 'Price' are musts in the equations that the pattern must match.
Try this, which matches all the input strings provided in the question:
/\(Price([+-]\d+%?){1,3}\)/g
You can test it in a regex fiddle.
Things to note:
Only use parentheses where you want to group. Parentheses around single-possibility, fixed-quantity matches (e.g. ([P]) provide no value.
Use character classes (opened with [ and closed with ]) for multiple characters that can match at a position in the pattern (e.g. [+-]). Single-possibility character classes (e.g. [P]) similarly provide no value.
Yes, character classes (generally) implicitly escape regex special characters within them (e.g. ( in [(] vs. equivalent \( outside a character class), but to just escape regex special characters (i.e. to match them literally), you are better off not using a character class and just escaping them (e.g. \() – unless multiple characters should match at a position in the pattern (per the previous point to note).
The quantifier {1} is (almost) always useless: drop it.
The quantifier + means "one or more" as you probably know. However, in a series of cases where you used it (i.e. ([(])+([P])+([r])+([i])+([c])+([e])+), it would match many values that I doubt you expect (e.g. ((((((PPPrriiiicccceeeeee): basically, don't overuse it. Stop to consider whether you really want to match one or more of the character (class) or group to which + applies in the pattern.
To match a literal string without any regex special characters like Price, just use the literal string at the appropriate position in the pattern – e.g. Price in \(Price.
/\(Price[+-](\d)+(%)?([+-]\d+%?)?([+-]\d+%?)?\)/g
works on http://www.regexr.com/
/^[(Price]+\d+\d+([%]|[)])&/i
try at your own risk!

Regular expression for matching 'A00000' format javascript

I need to match a substring in the format 'A00000', 'B12342', 'W33344' from a string. Basically, first char is always a letter, and following five are numbers.
I have got two regular expressions for this, and it looks like both work correct:
str.match(/[A-Z]{1}\d{5}/)
str.match(/^[A-Z]\d{5}/)
Which is more strict, and is there anything better than these two? Thanks.
To match the pattern as part of a bigger string instead of just at the start or anywhere in the string and still make them "strict", you can use the boundary meta character:
/\b[A-Z]\d{5}\b/g
The /g modifier does a global search and finds more matches.
Pattern explained
Example
> 'A12345 B43221'.match(/\b[A-Z]\d{5}\b/g)
["A12345", "B43221"]

Difficulties with constructing this JavaScript regex

I would like to construct a regular expression that matches any letter (including accented and Greek), number, hyphens and spaces with a total allowed characters length between 3 and 50.
This is what I made:
[- a-zA-Z0-9çæœáééíóúžàèìòùäëïöüÿâêîôûãñõåøαβγδεζηθικλμνξοπρστυφχψωÇÆŒÁÉÍÓÚŽÀÈÌÒÙÄËÏÖÜŸÂÊÎÔÛÃÑÕÅØΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]{3,50}
Now I wan't to adjust the expression so that it can't start with a hyphen or space. It will be used to validate a username.
I thought about using a negative lookbehind but these are the limitations:
JavaScript doesn't support a lookbehind.
The alternatives for a lookbehind aren't really applicable since they all depend on other JavaScript functions and I am bound to using the match function.
I hope there are any regular expression heroes here since it doesn't look simple.
I replaced your long character class with a-z for readability:
[a-z][- a-z]{2,49}
You could also match with your current regex and then make sure that the string does not match ^[ -] in another match.

Categories