split string in numeric values and non nummeric numbers regex - javascript

I've got this css string
var cssString = 'scale(0.95,0.95) rotate(45deg) translate(10px,11px)';
I want to use regex to return an array looking like this:
var array = ['scale(','0.95',',','0.95',') rotate(','45','deg) translate(','10','px,','11','px)'];
my last attempt:
var array = cssString.match(/([ a-zA-Z])*(\d*[.]?\d*)*/g);
I'm struggeling to have the first group match the parantheses aswell. How do I make this happen?

This regex will separately select both text (with special characters ()) and numbers (with dots .) and the commas:
([A-Za-z()])+|(,)+|([.\d])+

Related

Extract number and text from string with RegExp exec Javascript

I'm trying to extract a number and text from strings like those: 171Toberin, [171]Toberin or [171] Toberin.
I have this RegExp /(?<code>\d+)(?<name>\w+)/u, and this RegExp only works with 171Toberin.
You can use the below regex to remove all non alphanumeric characters.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.replace(/[^a-zA-Z0-9]/g, ''));
Or use the below to extract the alphanumeric characters from string.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[a-zA-Z0-9]+/g));
Or if you want to extract numbers and strings in separate array then use the below one.
let string = '171Toberin, [171]Toberin or [171]';
console.log(string.match(/[0-9]+/g));
console.log(string.match(/[a-zA-Z]+/g));
Please try with this: (?<code>\d+)[^\d\w]*(?<name>\w+)/ug
It works with the entire sentence: 171Toberin, [171]Toberin or [171] Toberin.
Returning 3 matches. You can try it at https://regex101.com/
With [^\d\w]* you omit possible numbers and words in between. With g flag you return all matches.

How can I parse with regex name/value pairs separated by a colon?

I have this string (faceted search query):
var q = ":salesRevenue:brand:Tchelicon:brand:Eurocom
:brand:Turbosound:brand:Labgruppen:brand:Tannoy:brand:Klarkteknik
:brand:Midas:brand:Bugera
:brand:Tcelectronic
:eligibleProduct:0010000066:publicProduct:false:brand:Behringer:productFilter:All:";
I want to split it to three separate strings: q_brand, q_pubs and q_product and my client wants it all in Regex.
q_brand consists of values with values preceded by "brand:" only.
q_pubs are values preceded by "eligibleProduct:" and "publicProduct:".
q_product are values preceded by "productFilter:".
I want to be able to capture them even if they interchange positions (ex. like the position of :brand:Behringer:)
The end result is expected to be:
var q_brand = "brand:Tchelicon:brand:Eurocom
:brand:Turbosound:brand:Labgruppen:brand:Tannoy:brand:Klarkteknik
:brand:Midas:brand:Bugera
:brand:Tcelectronic:brand:Behringer";
var q_pubs = "eligibleProduct:0010000066:publicProduct:false";
var q_product = "productFilter:All";
I tried using (brand:.+:) on brand:apple:brand:orange:product:hello:brand:grain: for one instance and several others but am getting unexpected results.
You were pretty close -- the main problem in your attempt was that you included the trailing colon in your match, which interfered with later matches.
The general pattern is (foo:[^:]+)/g) -- this will match a fieldname "foo", followed by a colon, followed by any number greater than zero of characters which aren't colons, repeatedly. Then the results are joined (again with colons) to fit your desired output.
for q_pub you have two different fieldnames, so is generalized to "\w+Product" (match any number of letter characters followed by Product.)
var q = ":salesRevenue:brand:Tchelicon:brand:Eurocom:brand:Turbosound:brand:Labgruppen:brand:Tannoy:brand:Klarkteknik:brand:Midas:brand:Bugera:brand:Tcelectronic:eligibleProduct:0010000066:publicProduct:false:brand:Behringer:productFilter:All:";
var q_brands = q.match(/(brand:[^:]+)/g).join(":");
var q_pub = q.match(/(\w+Product:[^:]+)/g).join(":");
var q_products = q.match(/(productFilter:[^:]+)/g).join(":");
console.log(q_brands);
console.log(q_pub);
console.log(q_products);

Return an array of strings after filtering #tags

I was wondering if someone could provide me with a JavaScript RegEx statement to filter out the text out of all the #tags in the input box.
Scenario: I have a user input text box where users can enter multiple #tags. What I would like to do is have all the texts filtered out and stored in an array after removing the special characters and save it to the database by looping over the array.
Example: Input- #tag1, #tag2, #tag3...
Output: An array of [tag1, tag2, tag3...]
Thanks in advance..
Use simply:
/(#\w+)+/gmi
And in your list of regex matches, you'll have all of the tags in an array. This expression only supports letters, numbers and underscores - simply adjust the \w if you want to extend or restrict the set of characters.
Here's a regex101 to play around with: https://regex101.com/r/pJ8vA4/2
The javascript would look something like:
var string = '#tag1, #tag2, #tag3 some other stuff #tag4';
var tags = string.match(/(#\w+)+/gmi);
tags = result.map(function(tag) { return tag.replace('#', '') });
console.log(tags);
This is a place to as for questions, not for someone to make you code. But I will still answer.
(\s?#[a-zA-Z0-9]+,?)+
-separated by an optional white-space and optional commas
-make sure to trim the white space off the beginning and end of the returned values (while looping)
-also remove the hash tag (1st character after trimming)
-link for your example https://regex101.com/r/rT2aC5/1
edit: also does not include special characters. Let me know if you need a special modification and I will do it real quick for you :)
var input = "#tag1, #tag2, #tag3";
var regex = new RegExp(/#(\w+),?\s?/gi);
var match = null;
var results = [];
while (match = regex.exec(input)){
results.push(match[1]);
}
This will give you a results array that has: ["tag1", "tag2", "tag3"]

how to check comma between two string and build a regex for that

I have a text box and I need to have validation that no commas allowed between two words like this given below eg B521,Baraghat. I want to have regext for that. How to do that in javascript.
A regex is not necessary for this check. This will make sure that your string does not contain a comma.
if(str.indexOf(',') != -1) {
<Invalid string logic>
}
If you insist on a regex:
if(str.match(/\,/)) {
<invalid string logic>
}
var patt=/[^,]+,[^,]+/
That will find a comma between two non-commas.
This matches both:
B521,Baraghat
and
B521 , Baraghat
If you want to find a comma between two words explicitly, you can do:
var patt=/\b,\b/
This will find a comma between two word boundaries.
This matches
B521,Baraghat
but not
B521 , Baraghat

Counting items based on multiple separators?

I need to count the number of email addresses that a user inputs. Those addresses could be separated by any of the following:
Comma followed by no space - a#example.com,c#example.com.com
Comma followed by any number of spaces (ie. someone might have a comma follow by 3 spaces or just 1) - a#example.com, c#example.com.com
Only white space - a#example.com c#example.com.com
New line
What's a good way to clean that up and reliably count the addresses?
I assume regular 'ole javascript could handle this, but for what it's worth I am using jQuery.
The simplest way is just replace all commas with whitespaces, then, split your string based on blank spaces. No need for conditions.
Here's a fiddle with an example on that.
var emails = input.split(/[\s,]+/);
FIDDLE
var str="YOUR_STR",
arr = [];
if( str.indexOf(',') >= 0 ) {
// if comma found then replace all extra space and split with comma
arr = str.replace(/\s/g,'').split(',');
} else {
// if comma not found
arr = str.split(' ');
}
var l = "a#example.com,c#example.com.com a#example.com, c#example.com.com a#example.com c#example.com.com";
var r = l.split(/ |, |,/);
Regular expressions make that fairly easy.
If there is change of more than one space, the regex can be changed a bit.
var r = l.split(/ +|, +|,/);

Categories