Suppose I have some text which I want to extract only the digits from, and then apply a formatting filter to it.
For example,
//extract only the digits
TheText = TheText.replace(/\D/g, '');
//apply formatting pattern
TheText = TheText.replace(/(\d{1})?(\d{2})?(\d{2})?(\d{2})?(\d{2})?/, '$1 $2 $3 $4 $5')
Basically, the output will look like this:
1 23 45 67 89
How can I combine these regular expressions into just one?
Thanks.
Note: I'm not looking to chain the statements using .replace(...).replace(...) statements, just the regex.
Least clever way: extract each digit separately.
.replace(/\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*(\d)\D*/,
"$1 $2$3 $4$5 $6$7 $8$9")
Related
I have a regular expression that allows you to arrange it in bracket groups, but there, as I need a maximum of 15 characters, and the value of the code is calculated dynamically, I would like to know if it is possible to somehow limit the size of the characters in the regular expression to 15?
My regexp
let code = 3 // can be 1,2,3,4 etc
someValue
.replace(/(?<!^)\+|[^\d+]+/g, '')
.replace(new RegExp(`(\\d{${code}})(\\d)`), '($1) $2')
.replace(/(\d{3})(\d)/, '$1-$2')
.replace(/(\d{4})(\d)/, '$1-$2')
.replace(/(-\d)/, '$1')
input
'123456789101112131415'
output
'(123)-456-7891-01112131415'
expected output ( max 15 digits )
'(123)-456-7891-01112'
Nit:
Perhaps if you have a more elegant solution to achieve this result instead of what I provided, that would be great
What I'm trying to accomplish:
Assuming an example string:
1 this is a1 my test 1a 12 string 12.123 whatever 1
I would like to have a Regex, that would give me all the occurrences of numbers (floats included), but I want it to skip the number if a letter (or more generally: non-number) precedes or follows it. So a1 and 1a would not match.
I've been struggling with this for a while, I got to this point (not ideal, because it also catches the preceding space):
/(^|\s)\d*\.*\d+/g
But this will also catch the 1a instance... I could also set up something similar, that would skip 1a, but would catch a1...
Can I accomplish this using regex matching?
You can use word boundaries with this regex:
/(?:\.\d+|\b\d+(?:\.\d+)?)\b/g
RegEx Demo
This is not a regex-only answer but maybe that's a good thing, we'll see.
The regex in use here is /^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/:
var testStr = '.1 this is a1 my test +5 1a 12 string -2.4 12.123 whatever . .02e1 5e5.4 1 1.4e5 1.2.3';
console.log('matches');
console.log(...testStr
.trim()
.split(/\s+/g)
.filter(word => /^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/
.test(word)
)
);
console.log('mismatches');
console.log(...testStr
.trim()
.split(/\s+/g)
.filter(word => !/^[-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:e\d+)?$/
.test(word)
)
);
For a simple, but not comprehensive solution (assuming numeric types used in the given example string, no negative numbers,scientific notation,etc.,), try this:
\d*\.*\d+
It removes the \s from your regex you developed, which matches the preceding space.
\d* will match all of the numbers (digits [0-9]).
Adding \.*\d+ will match floats (decimal followed by digits [0-9]).
Try this expression: (?<=^|\s)[-+]?(?:[0-9]*\.[0-9]+|[0-9]+)(?=$|\s) - Regex demo
JavaScript supported: (?:^|\s)([-+]?(?:[0-9]*\.[0-9]+|[0-9]+))(?=$|\s) - Regex demo
This expression supports floating-point numbers with an optional sign.
JS does not support positive lookbehind, so it was replaced by non-capturing group. The numbers are captured by the 1st group.
I'm looking for a regex (that I can implement in Javascript, so no lookbehinds) which will match all occurrences of a character, as long as it doesn't appear between two other characters. For example, I want to match all hyphens as long as they are not between plus signs.
----- // should match.
+---+ // should not match
---+---+--- // should only match the first 3 and last 3 characters.
I've tried adapting the method used in this post like so:
[-]+(?![^+]*\+)+
But it is not matching as desired. Any help is greatly appreciated.
P.S. Looking specifically for a REGEX solution. I realize this may not be the optimal solution but I'm specifically trying to improve my knowledge of regex.
If the delimiters (+ in your case) always come in pairs, you could use this:
var str = 'a-b-+-c-+-d-e'; // delimter is +
matches = str.match(/-(?=([^+]*\+[^+]*\+)*[^+]*$)/g, '');
console.log(matches); // 4 matches of `-`
This matches every hyphen that has an even number of pluses following it (could be zero).
Please try the following:
(?:^-+|-+$)
Regex101 demo: https://regex101.com/r/524iej/1
You can use use .replace to remove all text between 2 +s:
str = str.replace(/\+-*\+/g, '');
Example:
var str = '---+---+---';
str = str.replace(/\+-*\+/g, '');
//=> ------
So, js apparantly doesn't support lookbehind.
What I want is a regex valid in javascript that could mimic that behavior.
Specifically, I have a string that consists of numbers and hyphens to denote a range. As in,
12 - 23
12 - -23
-12 - 23
-12 - -23
Please ignore the spaces. These are the only cases possible, with different numbers, of course.
What I want is to match the first hyphen that separates the numbers and is not a minus sign. In other words, the first hyphen followed by a digit. But the digit shouldn't be part of the match.
So my strings are:
12-23
12--23
-12-23
-12--23
And the match should be the 3rd character in the 1st 2 cases and the 4th character in the last two.
The single regex I need is expected to match the character in brackets.
12(-)23
12(-)-23
-12(-)23
-12(-)-23
This can be achieved using positive lookbehind :
(?<=[0-9])\-
But javascript doesn't support that. I want a regex that essentially does the same thing and is valid in js.
Can anyone help?
I don't know why you want to match the delimiting hyphen, instead of just matching the whole string and capture the numbers:
input.match(/(-?\d+) *- *(-?\d+)/)
The 2 numbers will be in capturing group 1 and 2.
It is possible to write a regex which works for sanitized input (no space, and guaranteed to be valid as shown in the question) by using \b to check that - is preceded by a word character:
\b-
Since the only word characters in the sanitized string is 0-9, we are effectively checking that - is preceded by a digit.
(\d+.*?)(?:\s+(-)\s+)(.*?\d+)
You probably want this though i dont know why there is a diff between expected output of 2nd and 4th.Probably its a typo.You can try this replace by $1$2$3.See demo.
http://regex101.com/r/yR3mM3/26
var re = /(\d+.*?)(?:\s+(-)\s+)(.*?\d+)/gmi;
var str = '12 - 23\n12 - -23\n-12 - 23\n-12 - -23';
var subst = '$1$2$3';
var result = str.replace(re, subst);
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");