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")]);
Related
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
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:
split string only on first instance of specified character
(21 answers)
Closed 6 years ago.
I'm having a string like below that i would like to split only on the first ,. so that if i for instance had following string Football, tennis, basketball it would look like following array
["football", "tennis, basketball"]
This should do it
var array = "football, tennis, basketball".split(/, ?(.+)?/);
array = [array[0], array[1]];
console.log(array);
Inspiration: split string only on first instance of specified character
EDIT
I've actually found a way to reduce the above function to one line:
console.log("football, tennis, basketball".split(/, ?(.+)?/).filter(Boolean));
.filter(Boolean) is used to trim off the last element of the array (which is just an empty string).
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 answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 9 years ago.
RegExp gurus, heed my call!
This is probably super simple, but I've painted myself in a mental corner.
Taking a regular URL, split after the ?, which gives a string like variable=val&interesting=something¬interesting=somethingelse I want to extract the value of interesting.
The name of the variable I'm interested in can be a substring of another variable.
So the match should be
either beginning of string or "&" character
followed by "interesting="
followed by the string I want to capture
followed by either another "&" or end of string
I tried something along the lines of
[\^&]interesting=(.*)[&$]
but I got nothing...
Update
This is to be run in a Firefox addon on every get request, meaning that jQuery is not available and if possible I would like to avoid the extra string manipulation caused by writing a function.
To me this feels like a generic "extract part of a string with regex" but maybe I'm wrong (RegEx clearly isn't my strong side)
simple solution
var arr = "variable=val&interesting=something¬interesting=somethingelse".split("&");
for(i in arr) {
var splits = arr[i].split("=");
if(splits[0]=="interesting") alert(splits[1]);
}
also single line match
"variable=val&interesting=something¬interesting=somethingelse".match(/(?:[&]|^)interesting=((?:[^&]|$)+)/)[1]
function getValue(query)
{
var obj=location.search.slice(1),
array=obj.split('&'),
len=array.length;
for(var k=0;k<len;k++)
{
var elm=array[k].split('=');
if(elm[0]==query)return elm[1];
}
}
This function directly extract the query URL and return the corresponding value if present.
//usage
var get=getValue('interesting');
console.log(get);//something
If you're using the Add-on SDK for Firefox, you can use the url module:
https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/url.html
This is much better than using regex.