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
Related
I have a VERY long string containing code from a Rich Text Editor. I need to split this up into 4 parts to save it to the database. I am doing this.
var fullPart = $('#summernote').summernote('code').replace("'", "\'");
var markupStr = fullPart.substring(0, 3000000);
var markupStr2 = fullPart.substring(3000000, 3000000);
var markupStr3 = fullPart.substring(6000000, 3000000);
var markupStr4 = fullPart.substring(6000000);
markupStr, markupStr3 and markupStr4 all contain values, but markupStr2 is empty. What am I doing wrong?
var markupStr2 = fullPart.substring(3000000, 3000000);
Explanation : Start and End index are same in this that is why you are getting empty results.
Check here for more information.
str.substring(indexStart[, indexEnd])
indexStart The index of the first character to include in the returned
substring.
indexEnd Optional. The index of the first character to exclude from
the returned substring.
This is a simple mistake. fullpart.substring(3000000,3000000) would return a string of length of 3,000,000 - 3,000,000 characters (0 characters). The correct way to do this is:
var fullPart = $('#summernote').summernote('code').replace("'", "\'");
var markupStr = fullPart.substring(0, 3000000);
var markupStr2 = fullPart.substring(3000000, 6000000);
var markupStr3 = fullPart.substring(6000000, 9000000);
var markupStr4 = fullPart.substring(12000000);
I am building a simple search where query string can have wild cards '*'. Search terms can be like following:
animal
ani*
*mal
an*al
all above should return true if the word is 'animal'.
how this can be done in JS / jquery?
will appreciate for help.
rnv
The match on a string is simple:
var mystring = "This is my string";
var myregex = /*some regex*/
var results = mystring.match(myregex); // you can also write the regex directly as the argument of the match method, without using any variable.
So in your case you could have:
var mystring = "animal";
var myregex = new RegExp(inputQuery.replace(/\*/g, '.*'), 'gi'); // gi stands for 'global' and 'ignorecase' when applying the regex
var results = mystring.match(myregex);
Beware that .* matches zero or more (comes from the * whildcard) character, ANY character (comes from the .)
If you want to match zero or more letter, number or underscoreuse \w*, if you want to match one or more, use \w+, and if you want to match a specific number of letters, use \w{x} (\w{3} matches exactly 3 letters).
var str = "anim*";
var replaced = str.replace("*", ".*");
var regex = new RegExp(replaced);
var result = regex.test("animal");
console.log(result);
change the str variable to get the result as true or false;
Implemented version - https://jsfiddle.net/dpoqnacv/1/
var regexString = '^'+ $('#searchbox').val().replace("*",".*") + '$';
if(new RegExp(regexString).test('animal'))
$('#resultdiv').html('Matching...');
else
$('#resultdiv').html('Not Matching...');
You can just transform your wildcard into a RegExp and perform your search. Here is a simple example.
var search = document.getElementById("search");
var result = document.getElementById("result");
result.style.color = "red";
function fsearch() {
var str=search.value;
str = str.replace("*", ".*") //Transform your wildcard into a RegExp
result.innerHTML = "animal".match(new RegExp(str));
}
<label for="search">Search : <input name="search" id="search" type="text"/></label>
<input id="ok" type="button" value="ok" onclick="fsearch()"/>
Result : <div id="result"></div>
not bad answers but i think its easier with .test();
var str = "my dog is an animal";
/dog*anim*/.test(str); //returns true
/d*mal/.test(str); //returns true
etc
give it a try
I have the following string: 0-3-terms and I need to increment the 3 by 20 every time I click a button, also the start value might not always be 3 but I'll use it in this example..
I managed to do this using substring but it was so messy, I'd rather see if it's possible using Regex but I'm not good with Regex. So far I got here, I thought I would use the two hyphens to find the number I need to increment.
var str = '0-3-terms';
var patt = /0-[0-9]+-/;
var match = str.match(patt)[0];
//output ["0-3-"]
How can I increment the number 3 by 20 and insert it back in to the str, so I get:
0-23-terms, 0-43-terms, 0-63-terms etc.
You're doing a replacement. So use .replace.
var str = '0-3-terms';
var patt = /-(\d+)-/;
var result = str.replace(patt,function(_,n) {return "-"+(+n+20)+"-";});
Another option is to use .split instead of regex, if you prefer. That would look like this:
var str = '0-3-terms';
var split = str.split('-');
split[1] = +split[1] + 20;
var result = split.join('-');
alert(result);
I don't understand why you are using regex. Simply store the value and create string when the button is called..
//first value
var value = 3;
var str = '0-3-terms';
//after clicking the button
value = value+20;
str = "0-" + value + "-terms"
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")
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.