I have a string like below
string TagName = "Synergy-SunOptics-545-888-LLA Replacement" ;
On this, I am using a regular expression like below.
TagName =TagName.replace(/\d{3}-\d{3}-[A-Z]{3}\s([\w\s]*)$/,a+"-"+b+"-"+c+" $1");
What it does is, this regular expression replaces the 3 numeric values i.e (545-888) with the numeric values I provide, but this works only if I provide exactly 3 numeric values for each numeric set..ie..
It works:
"Synergy-SunOptics-676-454-LLA Replacement"
Its not working:
"Synergy-SunOptics-54-8884-LLA Replacement"
So, I need that this regular expression should work with any no. of numeric values provided.. for eg.. "1111-1111" or "11-1111" or "1-1111" or "1111-111" etc..
Simply use the + quantifier in place of the {3} quantifier. + matches one or more occurrences. Whereas {3} specifies exactly three occurrences.
Like so:
TagName =TagName.replace(/\d+-\d+-[A-Z]{3}\s([\w\s]*)$/,a+"-"+b+"-"+c+" $1");
Or alternatively, if you need to match a specific range of occurrences, you can use a range quantifier: {1,8} would match 1 to 8 occurences, {6, 15} would match 6 to 15 occurrences etc.
See the quantifier section at this site for more information on JavaScript flavored regex quantifiers javascript regex cheat sheet
TagName =TagName.replace(/\d+-\d+-[A-Z]{3}\s([\w\s]*)$/,a+"-"+b+"-"+c+" $1");
Related
I was looking for a way to extract Number/Numbers from a string.
Example:
const string = "Monthly Weather Review, Volume 129, Issues 9-12"
and i found a solution on this site. The problem is that i don't understand it very well.
Can someone explain me what actually happened in the line below ?
let res = string.match(/[+-]?\d+(?:\.\d+)?/g).map(Number); //return [129, 9, -12]
The regular expression:
[+-]?\d+(?:\.\d+)?
will match substrings which:
[+-]? - May start with a + or a - (character set, made optional by the following ?)
\d+ - Then, contains one or more digit characters (numbers from 0 to 9)
(?:\.\d+)? - Non-capturing group, made optional by ?:
\.\d+ - May match a literal ., followed by more digit characters
The only part of the pattern that isn't used by your input is the decimals part at the end. For an example, in the string
foo +12.34
it will match +12.34.
Regular expression matches extract substrings from a larger string, as an array. The .map(Number) uses Array.prototype.map to transform all elements of one array into another array - it turns the array of strings into an array of numbers.
For starters .match(regex) is a method on the String prototype that given a regular expression, will execute it and return to you the results or null if there aren't any matches. See this for more info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match
For the regular expression, /[+-]?\d+(?:\.\d+)?/g:
[+-]? match optionally a + or -
\d+ match one or more numerical digits
(?:\.\d+)? non-capturing group of . followed by digits (you're getting the decimal values here). This is optional with the ?. It looks like they only want integers here.
/g - g for global. all matches returned.
More info on regular expressions: https://www.rexegg.com/regex-quickstart.html
.map(Number) will iterate over each string item in the matches array (all matches are strings because that is what the first thing was) and coerce to number by using the Number() function.
More info on Number: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number
More info on .map: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
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 have a string of the following form "some_text_AAAABB_some_other_text". There is an arbitrary even number of 'A's in the string and "BB" is a fixed string that follows the 'A's. Assuming that there is 2n 'A's I would like to use a regex to replace the 'A's with a string of 'A's of length n.
For the following string
"some_text_AAAABB_some_other_text"
the result would be
"some_text_AABB_some_other_text"
Is it even possible to achieve this with regex?
I'm using V8 javascript to perform the transformation.
There are two scenarios: 1) number of As is even, 2) number of As is odd.
If you do not care if there is an even or odd number of As, just use
replace(/(A+)\1BB/g, "$1BB")
where (A+) matches and captures into Group 1 one or more As as many as possible and \1 matches the same substring (the same number as is captured into Group 1). Since BB is a fixed string, we just put it into the pattern as a literal.
See this regex demo
If you do not want to modify a string with odd number of As, you need
replace(/(^|[^A-Z])(A+)\2BB/g, "$1$2BB")
See this regex demo
Here, the first capture group captures the start of string ^ or any character other than [A-Z], the second capture group captures 1 or more As, and the backreference now has the ID = 2 - hence, \2 is used.
I'm altering the segment of a string in javascript and got the following working (first iteration -optional comma).
var foo = "wat:a,username:x,super:man"
foo.replace(/(username\:\w+)(?:,)*/,"go:home,");
//"wat:a,go:home,super:man"
The trick now is that I might actually replace a key/value with only the key ... so I need to capture the original group with both optional value + optional comma.
var foo = "wat:a,username:,super:man"
foo.replace(/ ????? /,"go:home,");
//"wat:a,go:home,super:man"
As a bonus I'd like the most concise way to capture both optional numbers/and letters (updating my original to also support)
var foo = "wat:a,username:999,super:man"
foo.replace(/ ????? /,"go:home,");
//"wat:a,go:home,super:man"
You need to replace the + (1 or more occurrences of the preceding subpattern) quantifier with the * (0 or more occurrences of the preceding subpattern).
See Quantifier Cheatsheet at rexegg.com:
A+ One or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
A* Zero or more As, as many as possible (greedy), giving up characters if the engine needs to backtrack (docile)
Besides, you are not using any of the capturing groups defined in the pattern, so I suggest removing them.
.replace(/username:\w*,*/,"go:home,")
^
And if you have just 1 optional ,, use just the ? quantifier (1 or 0 repetition of the preceding subpattern):
.replace(/username:\w*,?/,"go:home,")
^
Note that in case you can have any characters before the end of string or comma, you can also use Fede's suggestion of using a negated character class: /username:[^,]*,*/. The [^,]* matches any character (even a newline) other than a comma.
Also, please note that you do not need to escape a colon. The characters that must be escaped outside of character class to be treated as literals are ., *, +, ?, ^, $, {, (, ), |, [, ], \. See RegExp MDN reference.
I'm not sure if I understood your question, but if you want to match username:?? you can use below regex:
(username\:\w*)
Working demo
Update: As stribizhev, pointed in his comment \w* can do the trick, however if you want to extend the regex to any characters besides letters or numbers you can use:
(username\:[^,]*)
I have this String: value268_generated_number
I usually do this : ^value.*_generated_number
How will I match my regex but NOT with these numbers: 1,10 and 100 after the word "value" ?
e.g:
value1_generated_number
value10_generated_number
value100_generated_number
The expression should also meet generated numbers up to 50000.
You can use a negative lookahead:
^value(?!10{0,2}_).*?_generated_number
Also note that you should be careful using the * quantifier with .. It usually matches more than you want it to. Better replace * with a character class, e.g. \d*. I made the quantifier lazy, just to be on the safe side.
If your input is always of the form value<digits>_<digits>, then the final expression would be:
^value(?!10{0,2}_)\d*_\d+$