I'm trying to create an expression to collect anything but digits and the character *. I tried to use the expression \D[^*] but somehow it's retrieving the first digit after blank espaces. I tried this expression with the string 1234 1234 1234 **** and the matches were: ' 1', ' 1'. Can anyone tell me why would the expression collect the digits with the blank spaces?
Thank you.
Your regex ('\D[^*]') will match a Space (thats' not a digit) followed by a digit (that's not a star '*').
You can do several Things, the easiest is to include '\d' in the character Group, then it will Work, because both '\d' and '*' are excluded:
/[^\d*]/g
Now it will only match Spaces in your example.
A square brackets in regex are a set, and a square bracket with ^ mean not in set.
The \d should also be inside the brackets:
[^\d*]
https://regex101.com/r/WizvVh/1
Related
I want to match every word separated by comma, but it must not include a quote like ' or ".
I was using this regex:
^[a-zA-Z0-9][\!\[\#\\\:\;a-zA-Z0-9`_\s,]+[a-zA-Z0-9]$
However, it only matches a character and number and not a symbol.
The output should be:
example,example //true
exaplle,examp#3 //true, with symbol or number
example, //false, because there is no word after comma
,example //false, because there is no word before comma
##example&$123,&example& //true, with all character and symbol except quote
You can match 1+ times what is present in the character class. Then repeat 1+ times in a non capturing group (?: what is present in the character class, preceded by a comma.
^[!\[#\\:;a-zA-Z0-9`_ &$#]+(?:,[!\[#\\:;a-zA-Z0-9`_ &$#]+)+$
Regex demo
Note that you don't have to escape \!, \#, \: and \; in the character class, and that \s might also possibly match a newline.
I'm assuming you want the whole string to match perfectly with your conditions and return true then and then only.
These are the conditions-
Each word should be separated by a comma, said comma should have 2 valid words on each side
Words can contain anything except the 2 kinds of quotes (' and ") and whitespace characters (spaces and newlines).
The regex you would use is this- ^(?:[^,'"\s]+,[^,'"\s]+)+$, with the global flag (g) on.
Check out the demo here
Edit: As per request of being able to match only a single word.
This is the regex you would use for that- ^(?:(?:[^,'"\s]+,[^,'"\s]+)+|[^,'"\s]+)$
This will match words separated by a , as well as match just a single word.
The conditions for what qualifies as a word remains the same as aforementioned.
Quick explanation:-
^[^,'"\s]+,[^,'"\s]+$
This part matches 2 words separated by a comma, [^,'"\s]+ denotes a word
Wrapping that whole thing in ^(?:[^,'"\s]+,[^,'"\s]+)+$ simply makes it repeat, so it'll match N number of words separated by a comma, not just 2
Then adding another alternative using | and wrapping the whole thing in a group (non-capturing), we get ^(?:(?:[^,'"\s]+,[^,'"\s]+)+|[^,'"\s]+)$
This simply just adds the alternative [^,'"\s]+ - which matches a singular word.
Check out the updated demo here
The conditions of the regex are as follows:
Starts with either digits or a '+' sign and ends with digits.
This is going to be used to validate a certain type of number. What I got so far is:
/^\d*|\+\d*$/
This regex seems to match any string though. How would a regex that matches my conditions look like?
The regex will be used in a JavaScript function.
I think you want something like this,
^(?:[+\d].*\d|\d)$
^ Asserts that we are at the start.
[+\d] Matches a plus symbol or a digit.
.* Matches any character zero or more times.
\d Matches a digit.
| OR
\d A single digit.
$ Asserts that we are at the end.
Use this if you want to match also a line which has a single plus or digit.
^[+\d](?:.*\d)?$
DEMO
You need to use anchors ^ and $ on both sides of your regex and make first part + or digit) optional.
You can use this regex:
^([+\d].*)?\d$
RegEx Demo
I need to find a substring surrounded by double quotes, for example, like "test", "te\"st" or "", but not """ neither "\". To achieve this, which is the best way to go for it in the following
1) /".*"/g
2) /"[^"\\]*(?:\\[\S\s][^"\\]*)*"/g
3) /"(?:\\?[\S\s])*?"/g
4) /"([^"\\]*("|\\[\S\s]))+/g
I was asked this question yesterday during an interview, and would like to know the answer for future reference.
These expressions evaluate as follows:
Expression 1 matches:
An inverted comma
Greedily any character, including an inverted comma or a slash
A final inverted comma.
This would match "test" some wrong text "text", and therefore fails
Expression 2 matches:
An inverted comma
Greedily as many characters that are not either an inverted comma or a slash
Greedily as many sets of
Any chracter preceded by a slash
Greedily as many characters that are not either an inverted comma or a slash
A final inverted comma
So this collects all chracters within the inverted commas in sets, broken by slashes. It specifically excludes an inverted comma if it is preceded by a slash by including it in any subsequent sets. This will work.
Expression 3 matches:
An inverted comma
As few sets as fit of:
Any one character preceded by an optional slash
A final inverted comma
This collects all characters , optionally preceded by a slash, but not greedily. This will work
Expression 4 matches:
An inverted comma
Greedily all characters that are no either an inverted comma or a slash
One or more of:
An inverted comma or
A slash and any character
This will match "test"\x, and therefore fails
Conclusion:
From what I can tell, both expressions 2 and 3 will work. I may have missed something, but both will certainly work (or not as appropriate) for the examples given. So the question, then, is which is better. I'd vote for three, because it's simpler.
You could also get away with this simpler guy:
/("(\\"|[^"])+")/g
http://jsfiddle.net/b9chris/eMN2S/
Your grammar is a little unclear. I will assume that you want to find all strings of the form DQ [anything but DQ or \DQ]* DQ.
The regex for this /"([^"\\\\]|\\\\"|\\\\[^"])*"/g
I'm trying to match the second dot in a number to replace it later with a white space in my 'find and replace' function in Aptana.
I tried a lot of expressions, none of them worked for me.
For example I take the number:
48.454.714 (I want to replace the dot between 454 and 714)
Try this regex:
(\d{3})\.(\d{3})
and replace the first and second capturing group \1 \2
as mentioned by FiveO, you might want to match other numbers of digits too. E.g. one to 3 digits: \d{1,3} or any number of digits: \d+
Try with following regex:
\d+\.\d+(\.)\d+
And replace it with white space.
I want to restrict the user from entering same consecutive digits in a text field e.g User can't enter string like John22 or 22John or jo22hn....He can enter string like Joh2n2 , 2Joh2n and so on...All this has to be done in Javascript (Using regular expressions would be a better option)...Please help
Test a string for consecutive digits:
/(\d)\1/.test(string)
You can do this by using a negative lookahead.
^(?!.*(\d)\1).*$
See it here at Regexr
The ^ and the $ anchor the match at the start and the end of the string.
.* Will match everything (except newline characters)
The important part here is the Negative lookahead (?!.*(\d)\1) it will check the whole string for a digit \d put it in a capture group because of the brackets (\d) and reuse the value using the backreference \1 and the whole thing fails it there is a digit followed by the same digit.
The following regex should help:
/[0-9]{2,}/
Or
/[\d]{2,}/
Although, you can match for all instances using the /g flag:
/[0-9]{2,}/g
See it at this JSFiddle