extracting letters from an array of strings [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have an array of strings denoting variants of shirts in my store.
Here is an example string:
Apparel-DTG-Tshirt-District-DT6000-M-Black-Mens-CF-20200304113232677
All of the strings share same structure and the rule that I need to implement is, extract what's in between 5th and 6th occurrence of - (dash). In this particular case this is M.
How do I do that?

This would help you. The first capturing group will be the value between fifth and sixth dash:
https://regex101.com/r/UlQBqy/2
const re = /^(?:[^-]*-){5}(.*?)(?=-)/
console.log("Apparel-DTG-Tshirt-District-DT6000-M-Black-Mens-CF-20200304113232677".match(re)[1])

Related

How many elements in this array [,,]? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In Javascript,
can someone explain to me why
[,,] has 2 undefines elements and not three
[1,1,1] - has 3 elements so why [,,] has only two!
Actually it is from the definition in ES6, If you do not write anything after the last comma, then the item doesn't count for length.

Regular expression (Regex) to allow only number or specific character with number at a time [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a requirement to allow any digits with only one character (either w or d or W or D) at a time with no sequence.
For example: 2 or 2w or 222W or 2d or 222D
(not like 2wd, 55dw, 2w5d, 2w5w, 2D5D etc)
Could you please provide solution on this. Thanks.
Try the following:
^\d+[wdWD]?$
Try it out:https://regex101.com/r/sYqjTi/1
If you don't want it to match just 2, remove the ?, it will make the last letter required.
This regex should work both on Java and JavaScript.

Jquery comma separated array in one line to comma separeted array on multiple lines [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I do have an array inside Jquery(red array) on one line.
I do want it like the blue array seperated on multiple lines.
The red array needs to be like the blue array on the image.
Can some tell me how to do that, tried a lot of thing.
Try this:
var new_array = your_array[0].split(',');//["500,505,506...."]
console.log(new_array);//[500,505,506]

Regex with substitution around a number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a problem in some substitution in javascript (HTML).
I need to substitute ONLY the tags around every number, e.g.
<1>
become:
<sub>1</sub>
or
<20>
become
<sub>20</sub>
etc.
How can I do?
Thank you so much!
You can use replace and regex with groups to achieve this:
var text = '<1>blabla<20>';
var newText = text.replace(/<(\d+)>/g, '<sub>$1</sub>');
console.log(newText);

Replace string that contains dynamically numbers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
i have a variable that contains the string: attribute_value[XXX][] where XXX every time that page loads is a 3-digit number that changes every time.
How can i use .replace function in javascript to replace attribute_value[XXX][] with something specific regardless the XXX value?
Thank you in advance
You can use regular expression for that
str.replace(/attribute_value\d+/, 'aa');
here '\d' indicates digits and '+' indicates one or more occurances
str.replace(/attribute_value\d{3}/, 'aa');
Use this if you want to replace only first 3 digits

Categories

Resources