Split string by arithmetic operators [duplicate] - javascript

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 3 years ago.
I have a string "str1+str2-str3*str4".
I want to split it so I get an array
['str1','+','str2','-','str3','*','str4'].
Could someone help provide a solution?

If JS split lets capture groups become elements, then this should work
/([-+*\/])/
if not, I suggest using a regular find all type thing
using this
/([-+*\/]|[^-+*\/]+)/
otherwise, I'll just delete this.

Related

How to get all possible combinations of an array but with certain elements only changing (in Javascript) [duplicate]

This question already has answers here:
Cartesian product of multiple arrays in JavaScript
(35 answers)
How to utilise TypeScript Variadic Tuple Types for a Cartesian Product function?
(2 answers)
Closed 10 months ago.
Hi can anyone please help me. I want to be able to generate all possible combinations of the array but only certain elements will be changing given two possible numbers to choose from. Their position is important too.
For example: let arr = [1,2,3, (4 or 5), (6 or 7), (8 or 9)]
I want to generate
1,2,3,4,6,8
1,2,3,4,6,9
1,2,3,4,7,8
.
.
.
.
This goes on until 1,2,3,5,7,9
I know it is very similar to binary numbers, I just don't know how to go about it and also the array may change in size so it can lead to a lot of options and possible outcomes.
Please. Someone help.
Edit: clarified the combinations I want to generate

Javascript Regex: replace string with URL if not replaced already [duplicate]

This question already has answers here:
JavaScript - string regex backreferences
(5 answers)
Regex for detecting url in plain form and in markdown [closed]
(1 answer)
Closed 2 years ago.
I have the following problem that I'm trying to solve with String.replace:
I have a random text that contains IDs of tickets in some system e.g. JIRA-123. I want to replace then with Markdown links like so:
text.replace(/(JIRA-\d+)/g, "[$1](example.com?id=$1)");
The problem I'm struggling with is that I want this operation to be idempotent i.e. if a given ID is already wrapped in the URL I want it to be ignored and I'm struggling to figure out a correct expression to achieve that.
Any help?

How to translate ruby regex to javascript regex? [duplicate]

This question already has answers here:
Regex match entire words only
(7 answers)
Closed 3 years ago.
Im using validates_format_of method to check a input text
Javascript cant read this regex. How or where I can change this regex to be as original:
(?<=^|,|\b)[1-7](?=$|,|\b)
Thanks
UPDATE:
the input text must be one o more digits separated by comma, ex: 1|1,2|1,2,3
As #wiktor said you should use
\b[1-7]\b
As \b only asserts positions, you don't need to worry about matching more than [1-7].
#Code Maniac correctly stated that look behind is not supported in Mozilla and many others, so about it. see

How can create a regular expression for this URL? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 4 years ago.
I'm not good with RegEx and I would like have a RegEx for next URL scheme.
Can you help me to create a regular expression for that URL?
https://fonts/mapbox/{fontstack}/{range}.pbf
And if possible, show me one page to learn and understand it.
The best page I use to explain RegEx is https://regex101.com/.
In case your characters are all word characters, you can just use:
https://fonts/mapbox/(\w+)/(\w+).pbf
In the first group you will have "fontstack" and in the second you will have "range".
If you want to include a bit more of possible characters, maybe:
https://fonts/mapbox/([^\s\/])/([^\s\/]).pbf
You can see extensive explanations to both introducing them on the page I provided at the start.

Should I use single quotation mark or double to describe jquery? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
I'm confusing what I should use in jQuery.
Some use single some use double quotation mark.
For example
$('.class1').removeClass('class1')
$(".class1").removeClass("class1")
Also, alert as well
alert("aa");
alert('aa');
which one is correct?
This is more a matter of preference than correctness. See this question for more details: When to use double or single quotes in JavaScript?

Categories