So I currently have this:
var queries = window.location.search.slice( 1 ).split( "&" );
if(queries !== "") {
var count = 0,
........
........
It's getting all of the $_GET's that are submitted to a search. Well, for added functionality to display only the "active" attributes used in the search, I had to add more $_GET's and that's screwing up the above code because what it's running the split through is like this:
?search_type=physical&4=64_to_74_weight_1&active_4=true&10=70_to_84_weight_1&active_10=true&6=0_to_0_weight_1&7=0_to_0_weight_1&8=0_to_0_weight_1&9=0_to_0_weight_1&118=0_to_0_weight_1
What I need to do is exclude any "&active_#=true"s that are there and the amount of them won't always be the same.
Would I somehow use Regex for this? I'm not very familiar with it so I don't know where to start with the Regex bit.
Does anybody have any ideas or help for this?
You could through a .replace() in there before you split the value, to remove the entries . . . something like:
var queries = window.location.search.slice( 1 ).replace(/&active_\d+=true/g, "").split( "&" );
That should take care of getting rid of all of those "active" parameters for you.
Related
I am getting an array of objects using JSON and then my goal is to let the user search for a specific login. For this I want them to be able to type a letter and check each object login, if the letter is contained I want to display it.
In order to achieve this I worked on the following code:
var i;
var out="";
var exp=/d/g;
var result = " ";
for(i=0;i<users.length;i++){
result= exp.test(users[i].login);
if(result){
out+= users[i].login+ " ";
}
}
It works fine if I write the regex (in this case d) but once I try putting a variable inside the regex it wont work. How do I create a regex that will take the users input and work with the test function to perform the same task? Or idk if there is a better/more elegant solution for this. I know there are different regex questions already but I didn't find one that helped me.
Appreciate the help!
You're testing a literal string - that string is passed in by the user but it's still literal, not a regex.
So you should try:
if( users[i].login.indexOf(userInput) > -1)
This will pass if the given input is in the searched string.
I have an Array with one or more entries. Each one is a string (List of urls in open Tabs via Firefox SDK). I want to check if a specific url is already opened in some of the tabs (nothing special till now).
My problem is, that the url in tab list can have four diffrent fourms. For example:
Url I want to find in the tablist:
https://cmsr-author.de/cf#/content/test/de.html
But the url can also look like this:
https://cmsr-author.de/content/test/de.html
https://cmsr-author.de/test/de.html
https://cmsr-author.de/cf#/test/de.html
Of course the last part of the url (after /test/...) is always something diffrent. If I wasn't able to find one of the four urls in the tablist i want to call some other action.
My Solution till now is to build some if-chain:
if (res !== url1) {
if (res !== url2) {
if ...
But i thought there must be some more elegant way. Maybe via RegEx? I already have a capture to catch the first part (which stays the same https://cmsr-author.ws...) with it four forms. But i dont know how to implent this probably.
var urls = ["https://cmsr-author.de/content/test/de.html","https://cmsr-author.de/test/de.html","https://cmsr-author.de/cf#/test/de.html"]
var filtered = urls.filter(function(url)
{
return url.indexOf("cf#") > -1 && url.endsWith("/test/de.html")
})
var contains = filtered.length > 0
console.log(contains)
If you want to use regex you can do this by using groups for the middle part, which is explained in detail here: http://www.regular-expressions.info/refcapture.html
Practically, your regex would look something like that:
https:\/\/cmsr-author\.de\/(content|...|...)\/de\.html
Where ... must be replaced by the middle parts of the url which differ.
Note that | is "or" used to provide multiple possibilities within the group. The character / and . must be escaped since they have special roles in regex.
I hope that helps!
My English is not good,Do not fully understand what you mean,According to my idea,You should need a regular expression,Only to match the first.If I am wrong,
please # me.
I hope that helps!
var reg = /^https:\/\/cmsr\-author\.de\/cf#\/(?:\w+\/)+test\/de\.html$/gi;
var str1 = "https://cmsr-author.de/cf#/content/test/de.html";
var str2 = "https://cmsr-author.de/content/test/de.html";
var str3 = "https://cmsr-author.de/test/de.html";
var str4 = "https://cmsr-author.de/cf#/test/de.html";
console.log(reg.test(str1));
console.log(reg.test(str2));
console.log(reg.test(str3));
console.log(reg.test(str4));
Here is the url where I am getting lost. What is the issue?
I am getting the value as:
mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp?paramsgoes---
But sometimes it comes as:
http://mydomain.mymaindomain.com/asppage.asp?paramsgoes---
So I want to make sure that if the mydomain.mymaindomain.com/ comes extra, I want jquery to try to remove it. I am lost as to where I try to do it.
It is not showing http:// but it could be applying or it could not be applying, I'm not sure at this point.
One way would be to check whether the last occurrence of http is at the beginning of the string or somewhere in the middle. There's no need for jQuery, this is vanilla JS:
str = 'http://somedomain';
var pos = str.lastIndexOf('http');
if (0 != pos) {
str = str.substring(pos);
}
This will return http://mydomain.mymaindomain.com/asppage.asp? for:
http://mydomain.mymaindomain.com/asppage.asp?
http://mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp
mydomain.mymaindomain.com/http://mydomain.mymaindomain.com/asppage.asp
Demo
Try before buy
I'm trying to do something very simple, but I can't get to work the way I intend. I'm sure it's doing exactly what I'm asking it to do, but I'm failing to understand the syntax.
Part 1:
In the following example, I want to extract the part of the string between geotech and Input.
x = "geotechCITYInput"
x.match(/^geotech(.*)(?:Input|List)$/)
The result:
["geotechCITYInput", "CITY"]
I've been writing regex for many years in perl/python and even javascript, but I've never seen the ?: syntax, which, I think, is what I'm supposed to use here.
Part 2:
The higher level problem I'm trying to solve is more complicated. I have a form with many elements defined as either geotechXXXXInput or geotechXXXXList. I want to create an array of XXXX values, but only if the name ends with Input.
Example form definition:
obj0.name = "geotechCITYInput"
obj1.name = "geotechCITYList"
obj2.name = "geotechSTATEInput"
obj3.name = "geotechSTATEList"
I ultimately want an array like this:
["CITY","STATE"]
I can iterate over the form objects easily with an API call, but I can't figure out how to write the regex to match the ones I want. This is what I have right now, but it doesn't work.
geotechForm.forEachItem(function(name) {
if(name.match(/Input$/)
inputFieldNames.push( name.match(/^geotech(.*)Input$/) );
});
Any suggestions would be greatly appreciated.
You were missing the Input and List suffix in your regex. This will match if the name starts with geotech and ends with either Input or List and it will return an array with the text in the middle as the second item in the array.
geotechForm.forEachItem(function (name) {
var match = name.match(/^geotech(.*)(Input|List)$/);
if (match) {
inputFieldNames.push(match[1]);
}
});
I have some h3 element:
<h3>Data Performance</h3>
I need to change it to class name: data_performance.
So i do this but something is wrong, can anybody tell me what?
var $product_name = $('.subpage_promo.top').find('h3').text().toLowerCase(),
$product_name_mod = $product_name.split(' ').replace('_');
Thx for help.
You can do this using below code.
var $product_name = $('h3').html().toLowerCase(),
$product_name_mod = $product_name.replace(' ','_');
Demo
You're not implementing replace correctly. Its syntax is:
str.replace(regexp|substr, newSubStr|function)
It should be executed on a string (you're passing an array of strings), and it should specify both what to search for and what to replace it with (you're currently passing only what should be the second argument):
$product_name_mod = $product_name.replace(/ /g,'_');
The code above uses a regex, only to be able to specify the global flag, which is required in order to replace every space with an underscore. If you know you'll always have just one, .replace(' ','_') should do.
$('h3').addClass(product_name.replace(/ /g,'_'));
DEMO