What I want is simple. I have an input field and I want to check what items from an unordered list (ul) contain the input's value.
So, I have this:
$('input#search').keyup(
function() {
var value = $(this).val();
if (value.length > 0) {
var patt = /value/g;
console.log(patt);
$('ul#list li').filter(function() {
return (patt.test($(this).html()));
});
}
}
);
My problem here is that if, for example, the value of the input is 'abcd' patt will be /value/g instead of /abcd/g so I want to know how can I insert the value in the pattern.
To create a pattern based on dynamic data you'd use the RegExp ctor passing it a string of the pattern. e.g.
var word = 'foo';
var re = new RegExp(word, 'g'); // var re = /foo/g
Don't forget to escape characters with special meaning in regexp.
However, if you're just looking for a simple "is x within y" comparison, indexOf can perform this check very quickly:
var sample = 'this is a foo sentence';
var loc = 'Hello, world!'.indexOf('world') // 7
then simply check if indexOf(...) != -1.
define patt as new RegExp
var patt = new RegExp(value,"g")
Related
Assume that newFile[0] = 'Dany' and prsntFile[0] = 'Dany'. If newFile equal to present file then alert will work. My question is some time prsntFile[0] = 'Dany - 1' and my newFile[0] = 'Dany' but for this also i want to trigger the alert function.
prsntFile[0] may also contain dany -2 or dany -3. for this also i want to alert 'match'. any solution guys*
var newFile = 'Dany';
$(".copyDoc").each(function(){
var existFiles = this.value;
var prsntFile = existFiles.split('.');
if(newFile[0] == prsntFile[0]){
alert('match');
}elseif(newFile[0] == prsntFile[0]){
}
//alert(newFile[0]);
});
My Input hidden field
<input class="copyDoc" name="copyDoc[]" value="Dany - 1" type="hidden">
You can use, regex to do it.
var a = 'Dany'
var b = 'Dany 2'
var c = 'Day2'
var d = 'Dany-2'
var patt = new RegExp("^"+a+"");
console.log(patt.test(b));
console.log(patt.test(c));
console.log(patt.test(d));
The test() method tests for a match in a string.
This method returns true if it finds a match, otherwise it returns false.
So, I have created a regular expression object, patt and used that with test to find if there is a match in the string.
In regex : ^n Matches any string with n at the beginning of it
I am trying to get words from a string dynamically using a pattern. The pattern and input look something like this
var pattern = "hello this is %var% using %var%";
var input = "hello this is nick using javascript";
Now I want to be able to have an array of the variables like this
["nick", "javascript"]
This should do it:
var pattern = "hello this is %var% using %var%";
var input = "hello this is nick using javascript";
var re = new RegExp(pattern.replace(/%var%/g, '([a-z]+)'));
var matches = re.exec(input).slice(1); // <-- ["nick", "javascript"]
The variable re is a RegExp whose pattern is the pattern variable with each instance of %var% replaced with a capturing group of lower case letters (extend if necessary).
matches is then the result of the re regex executed on the input string with the first element removed (which will be the full matching string).
var pattern = "hello this is %var% using %var%";
var input = "hello this is nick using javascript";
var outputArr = [];
pattern.split(" ").forEach(function (e, i){
e === "%var%" ? outputArr.push(input.split(" ")[i]) : "";
});
outputArr is the desired array.
How do i replace the text with each method?
Is it right? it doesnt replace / find the text correctly
$(function(){
var style_find = ['tab-1','tab-2','rounded-1','rounded-2','tabs-1','tabs-2','tabs-alt-1','tabs-alt-2','tab-alt-1','tab-alt-2'];
var cur_style = $('#obj').attr('class');
var new_style;
$(style_find).each(function(i,cl){
new_style = cur_style.replace(cl,'new-class');
// it doesnt replace the whole word
});
})
String.prototype.replace() behaves differently depending upon the type of it's first parameter. Consider this code:
var example = "str str str str";
example = example.replace("str", "bob");
console.log(example === "bob str str str"); // === true?
I gave replace() a string for it's first parameter. When you do so, it only replaces the first occurrence of the substring.
When you call replace() with a RegExp you get something that returns all matches replaced
var example = "str str str str";
example = example.replace(/str/g, "bob");
console.log(example === "bob bob bob bob"); // === true
What we need is a regexp that matches everything you want to replace.
var style_find = ['tab-1','tab-2','rounded-1','rounded-2','tabs-1','tabs-2','tabs-alt-1','tabs-alt-2','tab-alt-1','tab-alt-2'];
var regexp = (function() {
var inner = "";
style_find.forEach(function(el) {
inner = inner + el + "|";
});
return new RegExp(inner, "g");
}());
With regexp I can modify your code to:
$(function(){
var style_find = ['tab-1','tab-2','rounded-1','rounded-2','tabs-1','tabs-2','tabs-alt-1','tabs-alt-2','tab-alt-1','tab-alt-2'];
var cur_style = $('#obj').attr('class');
var new_style = cur_style.replace(regexp, 'new-class');
});
Im trying to find a patterns in the sentence for regex matching.. in the code below result contains a string and we are checking if the word apple is present in it.
var patt = /apple/gi;
var newResult = patt.test(result);
I found the above code from a used case.. But i was wondering if i have more than one values and i want to check it in the string result, lets say an array with values var arr=["apple", "orange"] var patt=/arr[0]/gi will not work.. what could be the way in that scenario??
To check multiple entries, you can use the OR operator:
var patt = /apple|orange/gi;
var newResult = patt.test(result);
if you have a variable, you can do the below, IF(!) your key is regexp safe of course (that is, it doesn't contains characters which have meaning in regexp syntax):
var key = "apple";
var patt = new RegExp(key, 'gi');
var newResult = patt.test(result);
Although in this case, you might as well use indexOf:
var key = "apple";
var newResult = result.indexOf(key) > -1;
To use a string for your regex expressions, you need to create the regex using the regex constructor.
var pattern = "apple|orange";
var regex = new RegExp(pattern, "g"); // g is for global match
Read more about it here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
I have a string where I'm trying to grab the integer inside. The string looks like:
"(2) This is a string"
I need to grap that 2. but it could be any number so I tried:
var str = "(2) this is a string";
var patt = /\(\d\)/;
var num = str.match(patt);
This doesn't return the correct answer. Should I do a split on the () or is there a better regexp?
var str = "(2) this is a string";
var patt = /\((\d)\)/;
var num = str.match(patt)[1];
2 things. When you want to capture a segment form a matched string, you use () to note that. So I just wrapped the \d in parens to capture it.
Second, in order to access the captured segments, you must drill into the returned array. the match method will return an array where the first item is the entire matched string, and the second item is any matched capture groups. So use [1] to fetch the first capture group (second item in array)
Use this. doesnt matter how many parenthesis
var str = "(2) this is a string";
var patt = /\(\d\)/;
var num = str.match(patt)[0].replace("(", "").replace(")","")
This should work
var str = "(2) this is a string";
var a = /\([\d]*\)/g.exec(str)[0];
var num = a.substring(1, a.length-1);
var str = "(2) this is a string";
var patt = /\((\d+)\)/;
alert(str.match(patt)[1]);
This works!
Why it works. Because inside the (()) mayhem there's also a capture which populates the [1] elements in the matches array.