Regex to include white space and then verify a character - javascript

I have a pattern to search around web but im new to it and unable to verify it.
Im looking for example
[verify if any whitespace] [any of this char ':' '|' ';'] [verify if any whitespace] [[String a-zA-Z0-9-]+]
Suppose Test String -
" : hello129 " or ":hello129" or ";hello129" or "|hello129" or " | hello129"
My attemps
\s[:;|]\s[a-zA-Z0-9_.+-]+
(\w+\s\w+):(\w+\s\w+)[a-zA-Z0-9_.+-]+
Please suggest me possible solutions for this pattern in regex/regular expressions
Thank you in advance :)

Whitespace is represented with \s. The other groups are easily written in brackets.
Whitespace could by one or more characters, so the + modifier will be necessary. If Whitespace was optional, the * would have been okay as well. If only one character of whitespace would be allowed, we would leave the modifier out.
The string in the end is one or more characters long and needs the + as well.
The result is a regular expression like this:
\s+[:;|]\s+[a-zA-Z0-9-]+
Here is an example including tests on the great RegEx testing site regex101.com.

Related

RegExp - How to check if a string contains a substring in the certain position?

I tried to search this question first but didn't found what I need, here is it:
I have a string like: "substring1/substring2.substring3" (e.g. "library/History.read")
I need a regular expression to check:
the substring1 must be "library"
the substring2 must have a captial letter at the beginning
the substring3 must be "read"
I'm not familiar with regular expression, the current I wrote is:
'library/([a-zA-Z])\\.(read)' but it is not correct. Please help me, thanks!
There are many ways to solve regex problems, starting from your own attempt here's something that works! :-)
library/([A-Z][a-zA-Z]+)\.(read)
You had three small mistakes:
. is a character class matching any character except newline. Escape your . like this \. not like this \\.; and
Your first capture group was matching exactly one letter. You missed the quantifier. Use the + qunatifier to match one or more.
As pointed out by Peter, you were missing a character class for your first capital letter
Try this regex:
testStrings = ["library/History.read", "library/other.read", "library/Geography.read", "library/History.unread", "ebook/hstory.read", "library/StackOverflow.read"]
regex = /library\/[A-Z][^\.]*\.read/
testStrings.forEach(testString => {
console.log(regex.test(testString) + " - " + testString)
})

How to use Regex to tell if there are two non-alphanumeric charters together in string?

This is the requirement:
If field contains characters other than alphanumeric characters,
space, single quote, or dash without beginning or ending with a space,
or two non-alphanumeric charters together, system displays error
message
This is what I done.
regx = /^(?![ '-])[a-zA-Z '-]{1,30}([^ '-])$/;
It does all the job but not "two non-alphanumeric charters together". If I want to examine if there are two non-alphanumeric charters together in the string (eg. First Name), how to write that?
And I don't know much about what I write means actually, comments are welcomed, I just look online and write that regx...
And I don't know why if I remove {1,30}, the compiler will report error.
By non-alphanumeric charters, I mean [ '-]
I assume you want to match strings with length from 1 to 30.
I suggest a regex (demo) based on look-aheads to meet the requirements:
^(?![ ])(?!.*[ ]$)(?!.*[ '-]{2})[a-zA-Z0-9 '-]{1,30}$
See demo
Here, the 2 non-alphanumeric characters that cannot appear consecutively are [ '-]. The (?![ ]) look-ahead makes sure the string does not start with a space (replace [ ] with \s if any whitespace is meant, by the way). The (?!.*[ ]$) lookahead makes sure the string (without newline symbols - else, replace .* with [\s\S]*) does not end with a space. The (?!.*[ '-]{2}) lookahead makes sure there are no consecutive non-word symbols , ', or - in the string (again, use [\s\S]* instead of .* if you have newline symbols in the string).
You can also use an expression with just 2 look-aheads to minimize overhead:
^(?!.*[ '-]{2})(?![ ])[a-zA-Z0-9 '-]{0,29}[a-zA-Z0-9]$
Another demo
If you could allow minimum 2 characters, you could use a more optimal ^(?!.*[ '-]{2})[a-zA-Z0-9][a-zA-Z0-9 '-]*[a-zA-Z0-9]$, but I guess it is not the case.
Note in your regex, you have & instead of $ (end of string/line) and you did not allow digits (alphanumeric symbols usually include letters and digits).
If I want to examine if there are two non-alphanumeric charters
together in the string (eg. First Name), how to write that?
/[^a-zA-Z\d]{2}/g
I'll leave it to you to determine how to incorporate that into your regex. Check out this site for help building Regular expressions. It's saved me a bunch of times :)
This expression should work: /\W{2}|[^\w' -]|^ (.*) $/
var reg = /\W{2}|[^\w' -]|^ (.*) $/;
var strings = ['My own text', 'This. Is a bad string', 'This is also a bad string', ' This is also a bad string '];
strings.forEach(function(string){
document.getElementById('results').innerHTML += 'String: "' + string + '" is valid? ' + (string.match(reg) === null) + '\n';
});
<pre id="results"></pre>
^(?!.*\W\W)(?![ '-])[a-zA-Z '-]{1,30}([^ '-])$
^^^^^^^^^
Just add a lookahead to make sure non-alphanumeric charters \W doesnt come 2 times.
See demo.
https://regex101.com/r/sS2dM8/40

Regex to match a JSON String

I am building a JSON validator from scratch, but I am quite stuck with the string part. My hope was building a regex which would match the following sequence found on JSON.org:
My regex so far is:
/^\"((?=\\)\\(\"|\/|\\|b|f|n|r|t|u[0-9a-f]{4}))*\"$/
It does match the criteria with a backslash following by a character and an empty string. But I'm not sure how to use the UNICODE part.
Is there a regex to match any UNICODE character expert " or \ or control character? And will it match a newline or horizontal tab?
The last question is because the regex match the string "\t", but not " " (four spaces, but the idea is to be a tab). Otherwise I will need to expand the regex with it, which is not a problem, but my guess is the horizontal tab is a UNICODE character.
Thanks to Jaeger Kor, I now have the following regex:
/^\"((?=\\)\\(\"|\/|\\|b|f|n|r|t|u[0-9a-f]{4})|[^\\"]*)*\"$/
It appears to be correct, but is there any way to check for control characters or is this unneeded as they appear on the non-printable characters on regular-expressions.info? The input to validate is always text from a textarea.
Update: the regex is as following in case anyone needs it:
/^("(((?=\\)\\(["\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")$/
For your exact question create a character class
# Matches any character that isn't a \ or "
/[^\\"]/
And then you can just add * on the end to get 0 or unlimited number of them or alternatively 1 or an unlimited number with +
/[^\\"]*/
or
/[^\\"]+/
Also there is this below, found at https://regex101.com/ under the library tab when searching for json
/(?(DEFINE)
# Note that everything is atomic, JSON does not need backtracking if it's valid
# and this prevents catastrophic backtracking
(?<json>(?>\s*(?&object)\s*|\s*(?&array)\s*))
(?<object>(?>\{\s*(?>(?&pair)(?>\s*,\s*(?&pair))*)?\s*\}))
(?<pair>(?>(?&STRING)\s*:\s*(?&value)))
(?<array>(?>\[\s*(?>(?&value)(?>\s*,\s*(?&value))*)?\s*\]))
(?<value>(?>true|false|null|(?&STRING)|(?&NUMBER)|(?&object)|(?&array)))
(?<STRING>(?>"(?>\\(?>["\\\/bfnrt]|u[a-fA-F0-9]{4})|[^"\\\0-\x1F\x7F]+)*"))
(?<NUMBER>(?>-?(?>0|[1-9][0-9]*)(?>\.[0-9]+)?(?>[eE][+-]?[0-9]+)?))
)
\A(?&json)\z/x
This should match any valid json, you can also test it at the website above
EDIT:
Link to the regex
Use this, works also with array jsons [{...},{...}]:
((\[[^\}]{3,})?\{s*[^\}\{]{3,}?:.*\}([^\{]+\])?)
Demo:
https://regex101.com/r/aHAnJL/1

How to use user input as an Quantifier in Regex in JS?

My attempt was :
var re = new RegExp("\w{" + n + "}", "g");
But it didn't seems to work.
P.S. - I have searched several questions of Stackoverflow thinking it must have been asked before But I didn't find one, so I asked my question.
The problem is that \ is not only the escape character in regex but also in JS strings. So when you create a regular expression from a string you need to escape it. This means that \w becomes "\\w" in a string and if you want to match a single \ it would even become "\\\\".
Instead of changing it to \\w you can also use . if you don't care about the characters or if the string was validated before.

Create regex to isolate tags?

How could I achieve something like this?
example_input = "hi, this is example #input, it has a few #different things going #on. #question"
output ===> [input, different, on, question]
This is what I have so far:
text.match(/\#.+?\s+/g)
I've be able to get to isolation when followed by white space but I'm not sure how to adapt for , ' ' . or string end / new line
/#\w+\b/g
\b for boundary characters
for spaces, commas, and string ends
use '\s', ',' and '$'
/#\w+(\s?|.|$)/g
here is a list of special characters you can use (read under special characters):
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp
This will start matching at the # and go until either a space or punctuation is found.
/#[^\s\p{P}]+/

Categories