I'm trying to split a string after both the "&" and the ":". The string is formatted like this:
Public Protection & Housing Authority: $100,000,000.
In Javascript, I'm using .split(/(?=[:&]+)/g), which breaks the string before the & and :. How can I get it to break after each of those characters?
You can use lookbehind so it breaks after the match.
Regex:
/(?<=[:&])/g
Check here for the example I made: https://regex101.com/r/vi565o/1
+\w+ should do the trick.
It means to include letters after the is string is matched.
It should be - .split(/((&)+\w+)/g)
You can actually match strings up to and including these two chars if present, or up to end of string:
const s = 'Public Protection & Housing Authority: $100,000,000.';
console.log(s.match(/[^&:]*[&:]?/g).filter(Boolean));
The g flag makes String#match return all non-overapping matches found in the string. The regex matches
[^&:]* - zero or more chars other than & and :
[&:]? - an optional ampersand or colon char.
Related
I have a string which will be used to create a filename. The original string pattern may include a dash. Recently, the pattern has changed and I need to handle the regular expression to remove the dashes near the end or middle of the string but not those at the beginning of the string.
Regular Expression Pattern Rules/Requirements:
Replace all special characters with an underscore with some exceptions
Remove dashes not located at the beginning of the string
The dashes which need to be kept are typically between numeric values [0-9] and can appear any number of times in the string (i.e. "23-564-8 Testing - The - String" -> "23-564-8_testing_the_string")
The dashes which should be converted to underscores are typically between [a-zA-Z] characters (i.e. "Testing - The - String" -> "testing_the_string")
Examples of Potential Strings:
23-564-8 Testing the String -> Expected Output: 23-564-8_testing_the_string
Testing - The String -> Expected Output: testing_the_string
23-564-8 Testing - The - String -> Expected Output: 23-564-8_testing_the_string
Opinion: Personally, I'm not a fan of including dashes in filename but it is a requirement
Current Regexp Solution:
var str = "23-564-8 Testing the String";
str.replace(/[^a-zA-Z0-9-]/g, '_').replace(/__/g, '_');
Question: What is the best way to handle this case? My current solution leaves all dashes in the string.
You may use this regex with a negative lookahead:
/[^a-zA-Z0-9-]+|-(?!\d)/g
RegEx Details:
[^a-zA-Z0-9-]+: Match 1 or more of any character that is not hyphen or alphanumeric
|: OR
-(?!\d): Match hyphen if it is NOT immediately followed by a digit
Code:
const arr = [
'23-564-8 Testing the String',
'Testing - The String',
'-23-564-8 Testing - The - String'
]
const re = /[^a-zA-Z0-9-]+|-(?!\d)/g
var result = []
arr.forEach(el => {
result.push( el.replace(re, '_').replace(/_{2,}/g, '_') )
})
console.log( result )
The following Regex pattern can be used with a replacement string $1_ (see demo):
(\d+(?:-+\d+)+)?[\W\-_]+
The pattern consists of two parts:
(\d+(?:-+\d+)+)? captures numbers with allowed dashes into the Group1
[\W\-_]+ captures special characters to be replaced
The Group1 is required to prevent allowed dashes from being replaced. The $1 token in the replacement string ensures that this content of Group1 will be kept in the result.
This Regex pattern also handles the scenario of duplicate _ characters, so .replace(/__/g, '_') is no longer required. The code can be transformed to:
var str = "23-564-8 Testing the String";
var res = str.replace(/(\d+(?:-+\d+)+)?[\W\-_]+/g, "$1_");
console.log(res);
I want to validate an array of a single element of type string using regex. The array should not be empty and should not start with any special symbol except # and the string can include only numbers and alphabets.
what i tried
[a-zA-z0-9s!##$%^&*()_/\+=.,~`]+
you can try
regex = /^#?[a-zA-Z0-9]+/gm;
Try this:
regex = /^#?[a-z0-9]+/gi;
Breakdown:
/^ - Match the start of the string
#? - Match an optional #
[a-z0-9] - Character set including the lowercase alphabet, and all ten digits
+ - Match one or more of the preceding character set
/gi - Global and case-insensitive flags
I am trying Javascript's regular expression.
I understand that '|' is used to or-ing two regular expression.
I created a regex /^a*|b*$/, and I want it to detect any string that contains only charater of 'a' or 'b'.
But when I try /^a*|b*$/.test('c'), it produces true?
What I am missing understading of '|' operator?
Here's my code:
let reg = /^a*|b*$/;
< undefined
reg.test('c');
< true
| has very low precedence. ^a*|b*$ matches
either ^a*
or b*$
i.e. either a string beginning with 0 or more 'a's or a string ending with 0 or more 'b's. (Because matching 0 'a's is allowed by the regex, any string will match (because every string has a beginning).)
To properly anchor the match on both sides, you need
/^(?:a*|b*)$/
(the (?: ) construct is a non-capturing group).
You could also use
/^a*$|^b*$/
instead.
Note that both of these regexes will only match strings like aa, bbbbbb, etc., but not aba. If you want to allow the use of mixed a/b characters in a string, you need something like
/^(?:a|b)*$/
The OR in your example will split the expression in these alternatives:
^a* and b*$.
You can use groups to delimit the alternatives
Something like
/^(a*|b*)$/
This will match empty strings, strings that contains only a characters and strings that contain only b characters.
If you're looking to match strings that contain both a and b characters, you can use something like:
/^[ab]*$/
I'm developing a pattern that validates string if it does not contain more then two matches of #. here is code:
^[^\!|\#|\$|\%|\^|\&|\*|\+][\w]*([\w ]+\#[\w ]*){0,2}$
[^!|\#|\$|\%|\^|\&|*|+]
this is group of not acceptable symbols.
additionally, the pattern should validate string in case if it contains other symbols( - _ , . / ). each symbol should have it's own counter and should not match in any position more than two times.
for example if i have s string like this:
Mcloud dr. #33/#45, some text, some text
it should be valid. but in this case should not:
Mcloud dr. ###33/$#45, ####, ----
What would you suggest ?
Given that you want to match alphanumerics characters and some special symbols ()-_,./ You have to mention them in a character class like this.
Regex: ^(?!.*([(),.#/-])\1)([\w (),.#/-]+)$
Explanation:
(?!.*([(),.#/-])\1) asserts that there shouldn't be more than one character mentioned in character class. This asserts from beginning of string to end.
([\w (),.#/-]+) matches the rest of the string for allowed characters from beginning to end.
Regex101 Demo
I want to validate some strings.
Rules:
String can be contain A-Z, 0-9, "_", "!", ":".
If string contains 2x special characters, eg, "__" or "!!" or "K:KD:E" must return false.
Examples
Legitimate matches
FX:EURUSD
FX_IDC:XAGUSD
NYMEX_EOD:NG1!
Invalid matches:
0-BITSTAMP:BTCUSD - contains a minus sign)
2.5*AMEX:VXX+AMEX:SVXY - contains a *, a + and 2x ":"
AMEX:SPY/INDEX:VIX - contains a /
You can use this negative lookahead based regex:
/^(?:[A-Z0-9]|([_!:])(?!.*\1))+$/gm
RegEx Demo
([_!:])(?!.*\1) will ensure there is no repetition of special characters.
I would first start out with regex to remove all strings containing invalid characters:
/[^A-Z0-9_!:]/
I would then use this to check for duplicates:
/(_.*_)|(!.*!)|(:.*:)/
These can be combined to give:
/([^A-Z0-9_!:])|(_.*_)|(!.*!)|(:.*:)/
This can be seen in action here.
And here is a JSFiddle showing it working.
I eventrually used pattern
var pattern = /^[a-zA-Z_!0-9]+:?[a-zA-Z_!0-9]+$/;