Remove comma from group regex - javascript

Is it possible from this:
US Patent 6,570,557
retrieve 3 groups being:
US
Patent
6570557 (without the commas)
So far I got:
(US)(\s{1}Patent\s{1})(\d{1},\d{3},\d{3})
and was trying (?!,) to get rid of the commas then I effectively get rid of the whole number.

Try with:
var input = 'US Patent 6,570,557',
matches = input.match(/^(\w+) (\w+) ([\d,]+)/),
code = matches[1],
name = matches[2],
numb = matches[3].replace(/,/g,'');

Instead of using regex, you can do it with 2 simple functions:
var str = "US Patent 6,570,557"; // Your input
var array = str.split(" "); // Separating each word
array[2] = array[2].replace(",", ""); // Removing commas
return array; // The output
This should be faster too.

You cannot ignore the commas when matching, unless you match the number as three separate parts and then join them together.
It would be much preferable to strip the delimiters from the number from the matching results with String.replace.

Just add more groups like so:
(US)(\s{1}Patent\s{1})(\d{1}),(\d{3}),(\d{3})
And then concatenate the last 3 groups

Related

JS: remove end of string after nth character of type

I'm trying to write a script to remove the end of a string after a user inserts a number of special characters.
An example would be: Remove the end of a string from the 3rd comma, including the 3rd comma, so:
// Hi, this, sentence, has, a, lot, of commas
would become:
// Hi, this, sentence
I haven't been able to accomplish this with indexOf() because I don't know where the 3rd comma will occur in the sentence and I don't want to use split because that would create a break at every comma.
You can use split/slice/join to get the desired part of the string:
const str = "Hi, this, sentence, has, a, lot, of commas";
const parts = str.split(",");
const firstThree = parts.slice(0,3);
const result = firstThree.join(",");
console.log(result, parts, firstThree);
In a one-liner, that would be:
const result = str.split(",").slice(0,3).join(",");
Another simple option would be a regex:
const str = "Hi, this, sentence, has, a, lot, of commas";
const match = str.match(/([^,]+,){3}/)[0]; // "Hi, this, sentence,"
console.log(match.slice(0, -1));
This one uses the string variant of slice.
This is how the regex works:
In a capturing group (),
Find me at least one (+) character that is not a comma ([^,]): [^,]+
Followed by a comma ,
Now give me 3 of those groups after each other {3}
You can use below regex to get the result
const str = 'Hi, this, sentence, has, a, lot, of commas';
const m = str.match(/^(?:[^,]*,){2}([^,]*)/)[0];
console.log(m);

How can I split the word by numbers but also keep the numbers in Node.js?

I would like to split a word by numbers, but at the same time keep the numbers in node.js.
For example, take this following sentence:
var a = "shuan3jia4";
What I want is:
"shuan3 jia4"
However, if you use a regexp's split() function, the numbers that are used on the function are gone, for example:
s.split(/[0-9]/)
The result is:
[ 'shuan', 'jia', '' ]
So is there any way to keep the numbers that are used on the split?
You can use match to actually split it per your requirement:
var a = "shuan3jia4";
console.log(a.match(/[a-z]+[0-9]/ig));
use parenthesis around the match you wanna keep
see further details at Javascript and regex: split string and keep the separator
var s = "shuan3jia4";
var arr = s.split(/([0-9])/);
console.log(arr);
var s = "shuan3jia4";
var arr = s.split(/(?<=[0-9])/);
console.log(arr);
This will work as per your requirements. This answer was curated from #arhak and C# split string but keep split chars / separators
As #codybartfast said, (?<=PATTERN) is positive look-behind for PATTERN. It should match at any place where the preceding text fits PATTERN so there should be a match (and a split) after each occurrence of any of the characters.
Split, map, join, trim.
const a = 'shuan3jia4';
const splitUp = a.split('').map(function(char) {
if (parseInt(char)) return `${char} `;
return char;
});
const joined = splitUp.join('').trim();
console.log(joined);

Capture group certain number of times with regular expression but last group has remaining values

Given a string delimited by a colon and similar to this...
xvf:metric:admin:click
I need to capture three groups...
xvf
metric
admin:click
Or another example:
one:two:three:four:five:six:seven
one
two
three:four:five:six:seven
My current regex is just capturing each word separately, resulting in 4 matches
/(\s*\w+)/gi
The solution using String.match and Array.slice functions:
var str = "one:two:three:four:five:six:seven",
groups = str.match(/([^:]+?):([^:]+?):(.+)?$/).slice(1);
console.log(groups); // ["one", "two", "three:four:five:six:seven"]
And if it's possible that you get less than 3 groups, you can use
/^([^:]+)(?::([^:]+)(?::(.+)?)?)?$/
You can find an "explanation" of the RegExp here.
The solution is to capture the first two things before a :, then capture everything after
Here's the regex:
/(.+?):(.+?):(.+)/
In code:
var testStr = "xvf:metric:admin:click";
console.log(/(.+?):(.+?):(.+)/.exec(testStr).slice(1,4))
//["xvf", "metric", "admin:click"]
Since you're using JavaScript, it'd make more sense to actually use string.split and later Array.slice and Array.splice for string manipulation:
var str = "one:two:three:four:five:six:seven",
groups = str.split(':');
groups.splice(2, groups.length, groups.slice(2).join(':'));
console.log(groups);

JavaScript escape stars on regular expression

I am trying to get a serial number from a zigbee packet (i.e get from 702442500 *13*32*702442500#9).
So far, I've tried this:
test = "*#*0##*13*32*702442500#9##";
test.match("\*#\*0##\*13\*32\*(.*)#9##");
And this:
test.match("*#*0##*13*32*(.*)#9##");
With no luck. How do I get a valid regular expression that does what I want?
The below regex matches the number which has atleast three digits,
/([0-9][0-9][0-9]+)/
DEMO
If you want to extract the big number, you can use:
/\*#\*0##\*13\*32\*([^#]+)#9##/
Note that I use delimiters / that are needed to write a pattern in Javascript (without the regexp object syntax). When you use this syntax, (double)? quotes are not needed. I use [^#]+ instead of .* because it is more clear and more efficent for the regex engine.
The easiest way to grab that portion of the string would be to use
var regex = /(\*\d{3,}#)/g,
test = "*13*32*702442500#9";
var match = test.match(regex).slice(1,-1);
This captures a * followed by 3 or more \d (numbers) until it reaches an octothorpe. Using the global (/g) modifier will cause it to return an array of matches.
For example, if
var test = "*13*32*702442500#9
*#*0##*13*32*702442500#9##";
then, test.match(regex) will return ["*702442500#", "*702442500#"]. You can then slice the elements of this array:
var results = [],
test = "... above ... ",
regex = /(\*\d{3,}#)/g,
matches = test.match(regex);
matches.forEach(function (d) {
results.push(d.slice(1,-1));
})
// results : `["702442500", "702442500"]`

getting contents of string between digits

have a regex problem :(
what i would like to do is to find out the contents between two or more numbers.
var string = "90+*-+80-+/*70"
im trying to edit the symbols in between so it only shows up the last symbol and not the ones before it. so trying to get the above variable to be turned into 90+80*70. although this is just an example i have no idea how to do this. the length of the numbers, how many "sets" of numbers and the length of the symbols in between could be anything.
many thanks,
Steve,
The trick is in matching '90+-+' and '80-+/' seperately, and selecting only the number and the last constant.
The expression for finding the a number followed by 1 or more non-numbers would be
\d+[^\d]+
To select the number and the last non-number, add parens:
(\d+)[^\d]*([^\d])
Finally add a /g to repeat the procedure for each match, and replace it with the 2 matched groups for each match:
js> '90+*-+80-+/*70'.replace(/(\d+)[^\d]*([^\d])/g, '$1$2');
90+80*70
js>
Or you can use lookahead assertion and simply remove all non-numerical characters which are not last: "90+*-+80-+/*70".replace(/[^0-9]+(?=[^0-9])/g,'');
You can use a regular expression to match the non-digits and a callback function to process the match and decide what to replace:
var test = "90+*-+80-+/*70";
var out = test.replace(/[^\d]+/g, function(str) {
return(str.substr(-1));
})
alert(out);
See it work here: http://jsfiddle.net/jfriend00/Tncya/
This works by using a regular expression to match sequences of non-digits and then replacing that sequence of non-digits with the last character in the matched sequence.
i would use this tutorial, first, then review this for javascript-specific regex questions.
This should do it -
var string = "90+*-+80-+/*70"
var result = '';
var arr = string.split(/(\d+)/)
for (i = 0; i < arr.length; i++) {
if (!isNaN(arr[i])) result = result + arr[i];
else result = result + arr[i].slice(arr[i].length - 1, arr[i].length);
}
alert(result);
Working demo - http://jsfiddle.net/ipr101/SA2pR/
Similar to #Arnout Engelen
var string = "90+*-+80-+/*70";
string = string.replace(/(\d+)[^\d]*([^\d])(?=\d+)/g, '$1$2');
This was my first thinking of how the RegEx should perform, it also looks ahead to make sure the non-digit pattern is followed by another digit, which is what the question asked for (between two numbers)
Similar to #jfriend00
var string = "90+*-+80-+/*70";
string = string.replace( /(\d+?)([^\d]+?)(?=\d+)/g
, function(){
return arguments[1] + arguments[2].substr(-1);
});
Instead of only matching on non-digits, it matches on non-digits between two numbers, which is what the question asked
Why would this be any better?
If your equation was embedded in a paragraph or string of text. Like:
This is a test where I want to clean up something like 90+*-+80-+/*70 and don't want to scrap the whole paragraph.
Result (Expected) :
This is a test where I want to clean up something like 90+80*70 and don't want to scrap the whole paragraph.
Why would this not be any better?
There is more pattern matching, which makes it theoretically slower (negligible)
It would fail if your paragraph had embedded numbers. Like:
This is a paragraph where Sally bought 4 eggs from the supermarket, but only 3 of them made it back in one piece.
Result (Unexpected):
This is a paragraph where Sally bought 4 3 of them made it back in one piece.

Categories

Resources