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 have an adress such as https://stackoverflow.com/#/schedulePage/some/another , how can I make it to an array with elements after # ?
var urlToSplit = "https://stackoverflow.com/#/schedulePage/some/another"
var onlyAfterHash = urlToSplit.split("/#/")[1];
var result = onlyAfterHash.split("/");
console.log(result);
Use the split function
var str = "https://stackoverflow.com/#/schedulePage/some/another";
var res = str.split("#");
I think you just want something like this.First here I just split out the string by #, grab the second part of split result i.e index 1 then again splitting the result with / and finally filter out the empty string from the generated array.
var string = 'https://stackoverflow.com/#/schedulePage/some/another';
var result = string.split('#')[1].split('/');
var filtered = result.filter((entry)=>entry.trim() != '');
console.log(filtered);
Related
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 last year.
Improve this question
I have string data aa = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}
in javaScript and I've to extract the exact IP address --10.186.32.137 from this data
I'm trying this command--
b = aa.match(\10.186.32.137\g) but it also matches the pattern like 10.186.32.13. I need to match the exact pattern. Any help to fix this?
One of the most simple regex pattern would be:
const aa = `{"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]}`
const reg = new RegExp(/..\....\...\..../g)
const res = aa.match(reg)
console.log(res[0]);
but as posted in comments why not use JSON.parse?
Parse the string with JSON.parse(STRING) then access your network object ("PC-lab-network-452") JSON.parse(aa)["PC-lab-network-452"] then access any valid array index JSON.parse(aa)["PC-lab-network-452"][0] then access the addr property JSON.parse(aa)["PC-lab-network-452"][0].addr
If you want to solve this without regex. Try this :
const a = {"PC-lab-network-452":[{"version":4,"addr":"10.186.32.137","OS-EXT-IPS:type":"fixed","OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:39:38:ac"}]};
Object.keys(a).forEach(item => {
const ipExist = a[item].find(obj => obj.addr === "10.186.32.137");
if (ipExist) {
console.log(ipExist.addr);
}
else {
console.log('IP not found');
}
});
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 a search query like
`const query = '?sortBy=recent&page=2&comments=true&sortBy=rating' // two repeating params 'sortBy'
How can I use regex for checking is there any repeating params ????
Not recommended to use regex.
Try this
const query = new URLSearchParams('?sortBy=recent&page=2&comments=true&sortBy=rating');
const keys = [...query.keys()]; // convert iterable to array
console.log(keys)
const unique = keys.length === new Set(keys).size; // return false if dupes found
console.log(unique);
// to get the dupe(s)
const dupes = keys.filter((e, i, a) => a.indexOf(e) !== i)
console.log(dupes)
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 4 years ago.
Improve this question
I have an url that looks like this:
https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en
Is it possible to return every dk parameter separately? (no matter if there will be 1 or 5 dk parameters) so i would get separately sports, groupcompanyinsider, local.
If its not possible maybe there is a way to return all of them in one string like dk=sports&dk=groupcompanyinsiderlocal&dk=local ?
You can use the built-in javascript class URLSearchParams for this.
You can then transform this into the string you want with string concatenation and a foreach.
const url = "https://example.com/?category=123&dk=sports&dk=groupcompanyinsider&dk=local&lang=en";
var params = new URLSearchParams(url);
var result = "";
// concatenate individual values of the 'dk' query parameter
params.getAll('dk').forEach(function (item) {
result += '&dk=' + item;
});
result = result.substr(1); // remove starting '&' from the result;
console.log(result);
The result should contain your desired string.
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 8 years ago.
Improve this question
In my page there are some article tags with an attribute data-course-id.
<article class="register" data-course-id="0123"></article>
<article class="register" data-course-id="0124"></article>
Now I would like to generate a variable which contains an array from the article tags and the value should be the same as the data-course-id.
var array = [0123, 0124]
How can I do this?
Like this:
var array = $('.register').map(function(){
return $(this).attr('data-course-id'); // converting to Number will result in loss
// of data since what you are using is octal representation.
}).get();
Without jQuery, you can try something like this:
var articles = document.getElementsByClassName("register");
var array = [];
for(var i = 0; i < articles.length; i++) {
array.push(articles[i].getAttribute("data-course-id"));
}
Working demo.
try like this,
$('.register').each(function(){
arr.push($(this).attr('data-course-id'));
alert($(this).attr('data-course-id'));
});
Live Demo Here..
Using jquery, I would do something like this
Pushing the data values:
var array=[]
$(".register").each(function(){
array.push($(this).data('course-id'));
});
alert("pushing only the data values:" + array)
Pushing the objects:
array2 = []
$(".register").each(function(){
array2.push($(this))
});
alert("pushing the objects: " + array2)
http://jsfiddle.net/x9LdR/1
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 9 years ago.
Improve this question
I wanna filter some words of a statement with javascript.
I have words must be filtered and words must be replaced :
var words = [
{'badword':/dog/, 'goodword':'1'},
{'badword':/gav/ , 'goodword':'5'},
{'badword':/folan/ , 'goodword':':6'}
// .
//.
//.
//and more
];
For Example I wanna /dog/ filter and 1 replaced.
How i can?
http://jsfiddle.net/billymoon/RF7Gm/1/
var text = "this is a dog called gav and a thing which is a folan here.";
var words = [
{'badword':/dog/, 'goodword':'1'},
{'badword':/gav/ , 'goodword':'5'},
{'badword':/folan/ , 'goodword':':6'}
];
for(var i=0;i<words.length;i++){
var word = words[i];
text = text.replace(word.badword, word.goodword);
};
alert(text);
Should output something like... this is a 1 called 5 and a thing which is a :6 here.