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.
My first post on here. I have a function on a website whereby a randomly generated French phrase is displayed, challenging the reader to translate it into English in a text box. On clicking on a button, the text entered is compared to all the possible answers (there are multiple correct translations for a given phrase). I've looked around for answers on this but nothing seems to suit my situation.
Here's the jQuery:
var correctAnswer = function(){$('#correctmessage').show('fast');$('#errormessage').hide('fast');}
var wrongAnswer = function(){$('#errormessage').show('fast');$('#correctmessage').hide('fast');}
$('#1').find('button').on('click', function(){
var text = $(this).parent().find('.translatefield').val();
var compareText = "I went to the cinema";
var compareText2 = "I have been to the cinema";
if (text == compareText || text == compareText2) {
correctAnswer();
}
else {
wrongAnswer();
}
});
So I wondered if I can put the 'compare' variables into one variable i.e. 'I went to the cinema OR I have been to the cinema OR etc etc' within one variable for tidiness. But mainly I need to know how I can call that variable within the if so that it also accepts the answer without accented characters and regardless of upper or lower case... I hope this is clear! Thanks for any help you can give, this has been irritating me for a while!
As commented by Mark Holland, use arrays for the compare phrases.
If you are using jQuery anyway, you could use jQuery.inArray().
https://api.jquery.com/jQuery.inArray/
var compareText = ['i went to the cinema','i have been to the cinema'];
if ($.inArray(text.toLowerCase(), compareText)) {
... do stuff
}
To ignore the accents, use a solution like this:
String.prototype.removeAccents = function(){
return this
.replace(/[áàãâä]/gi,"a")
.replace(/[éè¨ê]/gi,"e")
.replace(/[íìïî]/gi,"i")
.replace(/[óòöôõ]/gi,"o")
.replace(/[úùüû]/gi, "u")
.replace(/[ç]/gi, "c")
.replace(/[ñ]/gi, "n")
.replace(/[^a-zA-Z0-9]/g," ");
}
Credits to Luan Castro
Perform a find/match with javascript, ignoring special language characters (accents, for example)?
As mentionned by Mark Holland, arrays would answer your first question.
JS arrays
To ignore accents, a quick search gave me this answer:
Replace accents
And to ignore lower/uppercase, a quick search gave me this answer.
Ignore case
How about setting the strings into one array and iterate this array to compare with the answer?
Im trying to create a regex for the following string:
"SEARCHCONTENT STTYPE_YU KAT_ALL CAMP_ALL STTYPE_LAVERE CAMP_1 KAT_71 KAT_79 KAT_81 "
Im creating the regex from a form with checkboxes so the user is able to filter the different values.
Lets say the user have checked 3 checkboxes with the values: KAT_ALL, KAT_71 & KAT_81
I then want to check those values against the string. I guess the regex im looking for is kind of like this: "KAT_ALL+AND/OR+KAT_71+AND/OR+KAT_81". How do I write this in JavaScript-regex format?
EDIT:
I got my code working after reading #yarons comment below. Now I have another case: I want to check if the string contains SSTYPE_YU AND (KAT_71 OR KAT_81).
I can't get the following regex to work, any ideas on why? (?=SSTYPE_YU)(?=KAT_71|KAT_81)
Basically, it is as simple as you wrote it yourself in the comments above.
If you want to create the Regex dynamically, you can first create a string, then create a regex out of that string, and then test.
For example:
var expressions = ["KAT_ALL", "KAT_71", "KAT_81"];
var regexStr = expressions.join('|');
var regex = new RegExp(regexStr);
regex.test(yourString); //returns true or false
You are close with (?=SSTYPE_YU)(?=KAT_71|KAT_81), use this one:
^(?=.*SSTYPE_YU)(?=.*(?:KAT_71|KAT_81))
I have a string:
Name1<br/>Name2<br/>Name3
Im looking to get a choice selector or an array with just the Names as values. I know you can get just the text of a string, but I cant figure out a way separate them. This list changes so I cant hard code the names in.
I cannot find any code nor do I have anything yet.
Use the split function:
var text = "Name1<br/>Name2<br/>Name3";
var list = text.split("<br/>");
This is easily accomplished using JavaScript built-in split().
var input_s = "Name1<br />Name2<br />Name3";
var input_r = input_s.split("<br />");
I got the String as :-Time in Queue,Item Type, Status,Type,Name, 22days, Document,Idle,Default,test4.
Now I have to compare status and its corresponding values as idle.
How to pick these two words in a single line and compare using java script.
If the structure is fixed, you could, for example, split the string
var result = "-Time in Queue,Item Type, Status,Type,Name, 22days, Document,Idle,Default,test4".split(",")[7] === 'Idle';
But I think you need something else, it is just unclear from the description. Probably if you added some context and described why you need this (why it must be single line, where the data came from, how they are really structured etc.), you could get a better answer.
...is there a line-break there, somewhere?
...you could do something like:
var string = "Time in Queue,Item Type,Status,Type,Name, 22days,Document,Idle,Default,test4",
pieces = string.split(","),
match = pieces[2].trim() === "Status" && pieces[6].trim() === "Idle";
...of course, .trim doesn't work on old browsers, but you can write something similar, easily.
If you really, really want to go golfing, you could do something like:
var string = "Time in Queue,Item Type,Status,Type,Name, 22days,Document,Idle,Default,test4",
reg = /[^,]+,[^,]+,\s*Status\s*,[^,]+,[^,]+,[^,]+,[^,]+,\s*Idle\s*/i;
reg.test(string);
This will tell you if "..., ..., Status, ..., ..., ..., ..., Idle" happens in the string, where "..." is any set of characters which isn't a comma.
If that's not what you mean, then you need to get more specific, here.