javascript insert after every digit - javascript

I am trying to add a space after every occurrence of a digit with javascript.
"2tim" will be "2 tim"
js
var v = '2tim';
v.replace(/(\d+)/, /\1 /);

There are 3 things wrong with your code:
The second argument to replace should be a string.
To use a captured group, use the dollar sign.
You don't want to capture all digits into the same group (\d+). Just capture one digit, and make the regex global.
var v = '2tim';
v = v.replace(/(\d)/g, '$1 ');
Here's the fiddle: http://jsfiddle.net/qujsq/
If you want to add a space only after a group of digits, then do use a +:
var v = '12times';
v = v.replace(/(\d+)/g, '$1 ');

Related

Regex expression to get numbers without parentheses ()

I'm trying to create a regex that will select the numbers/numbers with commas(if easier, can trim commas later) that do not have a parentheses after and not the numbers inside the parentheses should not be selected either.
Used with the JavaScript's String.match method
Example strings
9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4
What i have so far:
/((^\d+[^\(])|(,\d+,)|(,*\d+$))/gm
I tried this in regex101 and underlined the numbers i would like to match and x on the one that should not.
You could start with a substitution to remove all the unwanted parts:
/\d*\(.*?\),?//gm
Demo
This leaves you with
5,10
10,2,5,
10,7,2,4
which makes the matching pretty straight forward:
/(\d+)/gm
If you want it as a single match expression you could use a negative lookbehind:
/(?<!\([\d,]*)(\d+)(?:,|$)/gm
Demo - and here's the same matching expression as a runnable javascript (skeleton code borrowed from Wiktor's answer):
const text = `9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4`;
const matches = Array.from(text.matchAll(/(?<!\([\d,]*)(\d+)(?:,|$)/gm), x=>x[1])
console.log(matches);
Here, I'd recommend the so-called "best regex trick ever": just match what you do not need (negative contexts) and then match and capture what you need, and grab the captured items only.
If you want to match integer numbers that are not matched with \d+\([^()]*\) pattern (a number followed with a parenthetical substring), you can match this pattern or match and capture the \d+, one or more digit matching pattern, and then simply grab Group 1 values from matches:
const text = `9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4`;
const matches = Array.from(text.matchAll(/\d+\([^()]*\)|(\d+)/g), x=> x[1] ?? "").filter(Boolean)
console.log(matches);
Details:
text.matchAll(/\d+\([^()]*\)|(\d+)/g) - matches one or more digits (\d+) + ( (with \() + any zero or more chars other than ( and ) (with [^()]*) + \) (see \)), or (|) one or more digits captured into Group 1 ((\d+))
Array.from(..., x=> x[1] ?? "") - gets Group 1 value, or, if not assigned, just adds an empty string
.filter(Boolean) - removes empty strings.
Using several replacement regexes
var textA = `9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4
`
console.log('A', textA)
var textB = textA.replace(/\(.*?\),?/g, ';')
console.log('B', textB)
var textC = textB.replace(/^\d+|\d+$|\d*;\d*/gm, '')
console.log('C', textC)
var textD = textC.replace(/,+/g, ' ').trim(',')
console.log('D', textD)
With a loop
Here is a solution which splits the lines on comma and loops over the pieces:
var inside = false;
var result = [];
`9(296,178),5,3(123),10
10,9(296,178),2,5,3(123),3(124,125)
10,7,5(296,293,444,1255),3(218),2,4
`.split("\n").map(line => {
let pieceArray = line.split(",")
pieceArray.forEach((piece, k) => {
if (piece.includes('(')) {
inside = true
} else if (piece.includes(')')) {
inside = false
} else if (!inside && k > 0 && k < pieceArray.length-1 && !pieceArray[k-1].includes(')')) {
result.push(piece)
}
})
})
console.log(result)
It does print the expected result: ["5", "7"]

How to remove specific letters using regex in javascript

var cartstring = "27,00 - R"
How can I remove spaces and "-" and "R" using only regex (not allowed to use slice etc.)? I need to make strings cartstring1 and cartstring2 which should both be equal to "27,00", first by removing spaces and "-" and "R", and second by allowing only numbers and ",".
cartstring1 = cartstring.replace(/\s/g, "");
cartstring2 = cartstring.replace(/\D/g, "");
Please help me modify these regular expressions to have a working code. I tried to read about regex but still cannot quite get it. Thank you very much in advance.
you can just capture just what you are interested in number and comma:
let re = /[\d,]+/g
let result = "27,00 - R".match(re)
console.log(result)
You can group the characters you want to remove:
var cartstring = "27,00 - R"
let res = cartstring.replace(/(\s|-|R)/g, "")
console.log(res)
Or alternatively, split the string by a space and get the first item:
var cartstring = "27,00 - R"
let res = cartstring.split(" ")[0]
console.log(res)
You are using 2 replacements, one replacing all whitespace chars \s and the other replacing all non digits \D, but note that \D also matches \s so you could omit the first call.
Using \D will also remove the comma that you want to keep, so you can match all chars except digits or a comma using [^\d,]+ in a single replacement instead:
var cartstring = "27,00 - R";
console.log(cartstring.replace(/[^\d,]+/g, ''));

Regex to extract two numbers with spaces from string

I have problem with simple rexex. I have example strings like:
Something1\sth2\n649 sth\n670 sth x
Sth1\n\something2\n42 036 sth\n42 896 sth y
I want to extract these numbers from strings. So From first example I need two groups: 649 and 670. From second example: 42 036 and 42 896. Then I will remove space.
Currently I have something like this:
\d+ ?\d+
But it is not a good solution.
You can use
\n\d+(?: \d+)?
\n - Match new line
\d+ - Match digit from 0 to 9 one or more time
(?: \d+)? - Match space followed by digit one or more time. ( ? makes it optional )
let strs = ["Something1\sth2\n649 sth\n670 sth x","Sth1\n\something2\n42 036 sth\n42 896 sth y"]
let extractNumbers = str => {
return str.match(/\n\d+(?: \d+)?/g).map(m => m.replace(/\s+/g,''))
}
strs.forEach(str=> console.log(extractNumbers(str)))
If you need to remove the spaces. Then the easiest way for you to do this would be to remove the spaces and then scrape the numbers using 2 different regex.
str.replace(/\s+/, '').match(/\\n(\d+)/g)
First you remove spaces using the \s token with a + quantifier using replace.
Then you capture the numbers using \\n(\d+).
The first part of the regex helps us make sure we are not capturing numbers that are not following a new line, using \ to escape the \ from \n.
The second part (\d+) is the actual match group.
var str1 = "Something1\sth2\n649 sth\n670 sth x";
var str2 = "Sth1\n\something2\n42 036 sth\n42 896 sth y";
var reg = /(?<=\n)(\d+)(?: (\d+))?/g;
var d;
while(d = reg.exec(str1)){
console.log(d[2] ? d[1]+d[2] : d[1]);
}
console.log("****************************");
while(d = reg.exec(str2)){
console.log(d[2] ? d[1]+d[2] : d[1]);
}

Regex to validate and extract data from a calculation

I am trying to find out a regular expression where I can validate the input and also extract required information from input.
My input contains a simple calculation like addition, subtraction, multiplication and division.
For example: if input is addtion say 7.01+9.05
var input = '7.01+9.05';
var pattern = /^-?\d+\.?\d+[-+*\/]-?\d+\.?\d+$/
var sign;
if (input.match(pattern)) {
var matches = pattern.exec(input);
var left = // logic to extract value 7.01 using matches variable;
var right = // logic to extract value 9.05 using matches variable;
var sing = // logic to extract symbol + using matches variable;
}
I have used the regular expression which I found from this post : Calculator Regular Expression with decimal point and minus sign
Can you please help me how to extract the required data in above code?
In your pattern ^-?\d+\.?\d+[-+*\/]-?\d+\.?\d+$ you want to match an optional dot using \d+\.?\d+ which works but now the minimum number of digits to match is 2 due to matching 2 times 1 or more digits using \d+ so 1+1 would not match.
What you could do if it are only simple calculations, you could use 3 capturing groups and match a digit with an optional decimal part using ?\d+(?:\.\d+)?
Your pattern might look like:
^(-?\d+(?:\.\d+)?)([-+*\/])(-?\d+(?:\.\d+)?)$
Explanation
^ Start of string
(-?\d+(?:\.\d+)?) Capture group 1, match 1+ digits with an optional decimal part
([-+*\/]) Capture group 2, match any of the listed in the character class
(-?\d+(?:\.\d+)?) Capture group 2, match 1+ digits with an optional decimal part
$ End of string
See the regex101 demo
For example
var regex = /^(-?\d+(?:\.\d+)?)([-+*\/])(-?\d+(?:\.\d+)?)$/;
[
"21+22",
"7.01+9.05",
"1-1",
"1*1",
"0*1000000",
"8/4"
].forEach(x => {
var res = x.match(regex);
var left = res[1];
var right = res[2];
var sing = res[3];
console.log(left, right, sing);
});
Sure!
You should define capture groups in your regex expression using () and |. It is important define a flag global to your regex to capture all groups.
There are 3 things you need to capture:
the left number -> ^-?\d+\.?\d+
the sign -> [-+*\/]
the right number -> -?\d+\.?\d+$
You should use | alternation to regex use the capturing groups like a or statement beetwen the groups.
The final regex will be:
var pattern = /(^-?\d+\.?\d+)|([-+*\/])|(-?\d+\.?\d+$)/g
The ouput result will be an array where the first position will be the left number, second position the sign and the third position a right number.
Therefore the rest of your code will looks like that:
if (input.match(pattern)) {
var matches = input.match(pattern); \\ I recommend use input.match here too
var left = matches[0];
var right = matches[2];
var sing = matches[1];
}
You can do that using split()
var input = '7.01+9.05';
var pattern = /^-?\d+\.?\d+[-+*\/]-?\d+\.?\d+$/
if (input.match(pattern)) {
var matches = pattern.exec(input)[0].split(/(\+|-|\*|\/)/);
var left = matches[0];
var right = matches[2];
var sign = matches[1];
console.log(left,sign,right);
}

Javascript Regex: Capture between two asterisks with multiple asterisks in comma delimited string

I am trying to capture all characters between multiple instances of asterisks, which are comma delimited in a string. Here's an example of the string:
checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*
The caveat is that the phrase must start and end with an asterisk. I have been able to come close by using the following regex, however, it won't discard any matches when the captured string is missing the starting asterisk(*):
let str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*"
const regex = /[^\,\*]+(?=\*)/gi;
var a = str.match(regex)
console.log(a) // answer should exclude checkboxID0 and checkboxID5
The answer returns the following, however, "checkboxID0 and checkboxID5" should be excluded as it doesn't start with an asterisk.
[
"checkboxID0",
"checkboxID1",
"checkboxID3",
"checkboxID5"
]
Thanks, in advance!
You need to use asterisks on both ends of the pattern and capture all 1 or more chars other than commas and asterisks in between:
/\*([^,*]+)\*/g
See the regex demo
Pattern details
\* - an asterisk
([^,*]+) - Capturing group 1: one or more chars other than , and *
\* - an asterisk
JS demo:
var regex = /\*([^,*]+)\*/g;
var str = "checkboxID0*,*checkboxID1*,&checkboxID2&,*checkboxID3*,!checkboxID4!,checkboxID5*";
var m, res = [];
while (m = regex.exec(str)) {
res.push(m[1]);
}
console.log(res);

Categories