This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a string with a list of filenames such as
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp'
Is there a way to remove everything that doesn't end in .jpg so the output would look like this:
var newstring = '1.jpg,2.jpg,4.jpg'
You may write something like this
string
.split(",")
.filter(value => value.endsWith(".jpg"))
.join(",")
Did you experiment with possible regular expressions you could use? You might be able to find the answer yourself thanks to this page from the Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
If your string is always a comma separated list, then split the string on commas, which will give you an array of items. Then splice the array and remove items that contain the .jpg pattern.
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
string.split(',').filter((name)=> name.includes('.jpg')).join(',');
//"1.jpg,2.jpg,4.jpg"
var string = '1.jpg,2.jpg,3.png,4.jpg,5.webp';
var stringArray=string.split(',');
newArray=[];
stringArray.forEach(element => {
if(element.indexOf('.jpg')>-1){ newArray.push(element)}
});
console.log("jpg Array :"+newArray)// output : jpg Array :1.jpg,2.jpg,4.jpg
Related
This question already has answers here:
Find the characters in a string which are not duplicated
(29 answers)
Closed 1 year ago.
I found many question about counting characters used in a string and solutions
but how can I get all character used in string? Im new to JavaScript, im do not know to output this
for example
"English-Language" to "E,n,g,l,i,s,h,-,L,a,u,e" or "Javascript String" to "J,a,v,s,c,r,i,p,t, ,S,n,g"
Thank you..
Very simple with Set: (just cast it back to an array)
console.log([...new Set("English-Language")]);
console.log([...new Set("Javascript String")]);
And if you want it as a single string, do this:
console.log([...new Set("English-Language")].join(","));
console.log([...new Set("Javascript String")].join(","));
You can use the split function and the Set class to achieve this like so.
new Set("Javascript String".split(''))
The split('') part splits the string using an empty string as the seperator, so it just returns an array of all the characters. If you make a set from that array, it'll remove the duplicates. You can even pass the string directly to the Set constructor. If you want it as an array, just use Array.from.
you can simply use str.split("");
const str = 'English-Language';
str.split("");
Let me know if you face any future issue.
or Second option is
console.log([...new Set("English-Language")]);
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
I'm new to Regex and was wondering how to split this string s4://test-dev/prefixes/file.mxf into ['test-dev', 'prefixes/file.mxf']. I want it to work for an unknown file path.
Ex)
s4://test-dev/prefixes/file.mxf/newpath/anothernewpath into
['test-dev', 'prefixes/file.mxf/newpath/anothernewpath']
If you are using regex for splitting (which is not necessary) you can use this regex: s4:\/\/test-dev\/(.*) and use first group (first parentheses) as second string (first one is always same as i can see), but easiest way is to find position of third '/' with this var pos=str.split("/", i).join("/").length; and then find substring from that position to end: var res = str.substring(pos, str.length-pos);
Instead of splitting, use capture groups.
url.match(/^[^/]+:\/\/([^/]+)\/?(.*)/).slice(1)
Please check the code below:
let url = "s4://test-dev/prefixes/file.mxf/newpath/anothernewpath"
let match = url.match(/^s4:\/\/([\w-]+)\/(.+)$/)
let result = [match[1], match[2]]
console.log(result)
The result is:
[
"test-dev",
"prefixes/file.mxf/newpath/anothernewpath"
]
/^[\w+\:\/\/]+[-w+]/i
matches 'text-dev'
/w+\:\/\/[-w+]\/gi/
matches the rest
This question already has answers here:
Filter array by string length in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have a string for example:-
String: Notification 'Arcosa-Incident assigned to my group' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification's "Groups" field: 'Ruchika Jain' 9efa38ba0ff1310031a1e388b1050e3f
So basically i convert it into an array using .split(' ') method to make it comma separated values, now i want to filter this array and want only values which are 32 character long and remove rest of values.
Please help me achieve this. Alternate solutions are also welcomed. Thanks in advance.
Assuming you want to grab those IDs you can simply use a regex with match on the string without splitting/filtering it. (Note: I had to escape the single quotes in the text.)
const str = 'String: Notification \'Arcosa-Incident assigned to my group\' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification\'s "Groups" field: \'Ruchika Jain\' 9efa38ba0ff1310031a1e388b1050e3f';
const matches = str.match(/[a-f0-9]{32}/g);
console.log(matches);
Like so:
var arr = ...;
var filtered = arr.filter(word => word.length === 32);
Edit: this may be a bad idea if you want to parse only the GUIDs. It could certainly be that a name like "Ruchika" is also 32 characters long. Maybe, consider using regular expressions instead.
This question already has answers here:
Extract numbers from a string using javascript
(4 answers)
Closed 6 years ago.
I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
Use String.prototype.match()
The match() method retrieves the matches when matching a string against a regular expression
Edit: As suggested by Tushar, Use Array.prototype.map and argument as Number to cast it as Number.
Try this:
var exp = /[0-9]+/g;
var input = "23a43b3843c9293k234nm5g%>";
var op = input.match(exp).map(Number);
console.log(op);
var text = "23a43b3843c9293k234nm5g%>";
var regex = /(\d+)/g;
alert(text.match(regex));
You get a match object with all of your numbers.
The script above correctly alerts 23,43,3843,9293,234,5.
see Fiddle http://jsfiddle.net/5WJ9v/307/
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
array join() method without a separator
I'm using .join() to convert my array to a string so I can output it in a text box as the user selects numbers in a calculator, I'm not entirely sure how I can remove the commas that are also being output in the list however. Can someone advise how this can be achieved or if there is a different approach I should be using?
JS
$(document).ready(function() {
var total = 0;
var arr = [];
//Testing
$('#calculator').children('.num').on('click', function(e) {
var clickedNumber = $(this).data('id');
arr.push(clickedNumber);
console.log(arr.join());
e.preventDefault();
});
});
JS Fiddle http://jsfiddle.net/CVr25/
Simply like that:
arr.join("")
You can specify an empty string as an argument to join, if no argument is specified a comma is used.
arr.join('');
http://jsfiddle.net/mowglisanu/CVr25/1/
The .join() method has a parameter for the separator string. If you want it to be empty instead of the default comma, use
arr.join("");
All you need to do is :
arr.join('');
FIDDLE