I am trying to use Regex to test if a certain string contains only sets of four non-duplicate characters.
For example I would like to test string
acbdbcaddacb
which would return true as it can return
acbd
bcad
dacb
i.e. sets of four characters which have no duplicates even though the entire string does.
I have tried the following regex which does not work for example and I am not sure why:
/^(?:(?:([a-d])(?!.{0,2}\1))(?:([a-d])(?!.{0,1}\1))(?:([a-d])(?!\1))[a-d])+$/
Any solutions?
Thank you
You're close. Your current regex is only checking if the 2nd - 4th letters in each group match the 1st. I believe /^(?:(?:([a-d])(?!.{0,2}\1))(?:([a-d])(?!.{0,1}\1|\2))(?:([a-d])(?!\1|\2|\3))[a-d])+$/ should work... or at least it's getting closer to correct I'm not sure if I left out some edge cases but it seems to be working for my test strings
Try this :
function check(str) {
var len = str.length; // check string length
if (len % 4 == 0) { // pass if divided by 4 == true
var arr = str.match(/.{4}/g); // make the in array
var res = [];
for (i = 0; i < arr.length; i++) {
if (arr[i].match(/^(?:([a-zA-Z])(?!.*\1))*$/)) {
res.push(arr[i]); // push if passed regex
}
}
if (arr.length === res.length) { // if they same that means true
console.log("true");
} else {
console.log("false");
}
} else {
console.log("false");
}
}
var str1 = "acbdbcaddacb";
check(str1); // true
var str2 = "aabbccdd";
check(str2); // false
var str3 = "abcde";
check(str3); // false
var str4 = "abcdabcdabcdabcd";
check(str4); // true
var str5 = "abcdabcdabcdabc4";
check(str5); // false
Related
Not sure what i did wrong. I'm trying to compare 2 strings to see if they are Palindromes
function palindrome(str) {
var rem = str.replace(/\s/g, "");
var a = rem.split("");
var b = a.reverse();
for(var i = 0; i < rem.length; i++){
if (b[i] == a[i]){
return true;
}
return false;
}
}
palindrome("not a doctor"); //this should show false, but it's showing true
The reverse method transposes the elements of the calling array object in place, mutating the array, and returning a reference to the array.
Source
That's why you get true. Seems your a and b variables point to the same object.
By the way, your approach seems somewhat flawed as others have pointed out bellow. So, a better version of solution to your task could be (not properly tested):
function isPalindrome(input) {
var str = input.replace(/\s/g, "").toLowerCase();
var left = 0, right = str.length - 1;
while( left <= right) {
if(str[left] !== str[right]) return false;
left++; right--;
}
return true;
}
console.log(isPalindrome("your string")); // false
console.log(isPalindrome("Drawn onward")); //true
The idea is that you compare the corresponding symbols on both ends of your modified (without spaces and lowercased) string. If they don't match at some point, so this is not a palindrome.
There are a few problems with your code.
Problem 1: You are using reverse method which mutate the original array (See the docs on reverse). So variables a and b would have the same value, that is the reversed string of the original array. What you can do instead is create a fresh new array and assign it to the variable b like so:
var b = [].concat(a).reverse();
Problem 2: You forgot to check ALL of the letters in the string. You are returning your function way too early. For example, for the input string 'aada', which is not a palindrome it will return true. This is because, your function exits as soon as evaluates the similarity of the first string of both arrays. To fix this you could do something like this:
function palindrome(str) {
var rem = str.replace(/\s/g, "");
var a = rem.split("");
var b = [].concat(a).reverse();
for(var i = 0; i < rem.length; i++){
if (b[i] !== a[i]){
return false;
}
}
return true
}
You can even further optimise your function like this:
function palindrome(str) {
const len = str.length;
const halfLen = len/2;
for (let i = 0; i < halfLen; i++) {
if (str[i] !== str[len - 1 - i]) return false;
}
return true;
}
Palindrome strings read the same backwards and forwards, So you can make a comparison of the first and the last, second to second last etc until you reach the middle character.
Hope this helps.
As others have said, your a and b points to same object, so you need to clone it first.
Also you must not return true immediately, better way is to check for inequality and return true after the whole cycle ends.
function palindrome(str) {
var rem = str.replace(/\s/g, "");
var a = rem.split("");
var b = a.slice().reverse();
console.log(a, b);
for(var i = 0; i < rem.length; i++){
if (b[i] != a[i]){
return false;
}
}
return true;
}
console.log(palindrome("lol 1"));
Your code had two problems.
As noted by #curveball, your a and b variables are references to the same objects, as reverse modifies the original array.
Additionally, you are returning true as soon as the first element in the a array is equal to the first element of the b array. You must return false as soon as one element is different from another. But can only return true after comparing all the elements in the array.
function palindrome(str) {
var rem = str.replace(/\s/g, "");
var a = rem.split("");
var b = a.slice().reverse();
for(var i = 0; i < rem.length; i++){
if (b[i] !== a[i]){
return false;
}
}
return true;
}
palindrome("not a doctor");
Additionally, an alternative (and typical) algorithm for checking whether a string is a palindrome would be to compare (after removing blanks) first position to last one, second position to second from the end, etc. That way it is not necessary to clone (and reverse) the data.
I have two arrays:
enteredCommands = ["valid", "this", 1.1];
validParameters = [/valid/, /alsoValid/, /this|that/, /\d+(\.)?\d*/];
I want to loop through all of the enteredCommands, if it exists in validParameters remove it from validParameters, if it doesn't, break.
I don't know how to compare the regex in this way, if I change validParameters to:
validParameters = ["valid", "alsoValid", /this|that/, /\d+(\.)?\d*/];
and use:
var ok2go = true;
// For each entered command...
for (var i = 0; i < commands.length; i++) {
// Check to see that it is a valid parameter
if (validParameters.indexOf(commands[i]) === -1) {
// If not, an invalid command was entered.
ok2go = false;
break;
// If valid, remove from list of valid parameters so as to prevent duplicates.
} else {
validParameters.splice(validParameters.indexOf(commands[i]), 1);
}
return ok2go;
}
if (ok2go) {
// do stuff if all the commands are valid
} else {
alert("Invalid command");
}
It works the way I want it to for the strings, but obviously not for those values that need to be regex. Is there any way to solve this?
Test cases:
enteredCommands = ["valid", "this", 1.1, 3];
// Expected Result: ok2go = false because 2 digits were entered
enteredCommands = ["valid", "alsoValid", "x"];
// Expected Result: ok2go = false because x was entered
enteredCommands = ["valid", "alsoValid", 1];
// Expected Result: ok2go = true because no invalid commands were found so we can continue on with the rest of the code
You could filter the given commands and if a regular expression match, the exclude it from the regex array. Return only commants which does not match with the rest of the regex array.
function check(array) {
var regex = [/valid/, /alsoValid/, /this|that/, /\d+(\.)?\d*/];
return array.filter(function (a) {
var invalid = true;
regex = regex.filter(function (r) {
if (!r.test(a)) {
return true;
}
invalid = false;
});
invalid && alert('invalid command: ' + a);
return invalid;
});
}
console.log(check(["valid", "this", 1.1, 3])); // 2 digits were entered
console.log(check(["valid", "alsoValid", "x"])); // x was entered
console.log(check(["valid", "alsoValid", 1])); // nothing, no invalid commands were found
I recommend you split up the checks for a matching regex and the check for a matching string.
Conceptually, you might want to do the following (code not tested)
var validStringParameters = ["valid", "alsoValid"];
var validRegexMatches = [/valid/, /alsoValid/, /this|that/, /\d+(\.)?\d*/];
var validCommands = enteredcommands.filter(function(command){
if (validStringParameters.indexOf(command) !== -1){
return true;
}
for (var i = 0; i < validRegexMatches.length; i++){
if (command.test(validRegexMatches[i]){
return true;
})
return false;
}
})
I have the following two strings:
var str1 = "hello";
var str2 = "ehlol";
How can I check whether both strings contain the same characters?
May not be very optimal, but you can simply do
str1.split("").sort().join() == str2.split("").sort().join(); //outputs true
Another suggested approach in one the comments (for optimization in case string length is quite big)
str1.length===str2.length && str1.split("").sort().join() == str2.split("").sort().join(); //first check the length to quickly rule out in case of obvious non-matches
One of the recommended ways to do it is using a hash table: count how many times each character appears. Note that this works best if your characters are ASCII.
The complexity of this algorithm is O(M+N+sigma) where M, N are the lengths of the strings and sigma is the number of distinct letters. The complexity of the accepted solution is higher because of the sorting, which is usually done in O(N*logN), but still a good one if your strings are short. If your strings have hundreds of thousands of characters, then this is the way to go. The drawback of using hash tables is that the memory usage is higher than the solution that uses sorting.
function sameLetters(str1, str2){
var hash = {};
var len1 = str1.length;
var len2 = str2.length;
// Strings with different lengths can't contain the same letters
if(len1 !== len2) return false;
// Count how many times each character appears in str1
for(var i = 0; i < len1; ++i) {
var c = str1[i];
if(typeof hash[c] !== 'undefined') hash[c]++;
else hash[c] = 1;
}
// Make sure each character appearing in str2 was found in str1
for(var i = 0; i < len2; ++i) {
var c = str2[i];
if(typeof hash[c] === 'undefined') return false;
if(hash[c] === 0) return false;
hash[c]--;
}
// Make sure no letters are left
for(var c in hash) {
if(hash[c]) return false;
}
return true;
}
You can then call the function like this (play with it in the browser console):
sameLetters("hello", "ehlol"); // true
sameLetters("hello", "ehllol"); // false
You can use a function for this purpose like sameChars function here-
function myFunction()
{
var input_1 = document.getElementById('input_1').value;
var input_2 = document.getElementById('input_2').value;
var result = sameChars(input_1,input_2);
document.getElementById("demo").innerHTML = result;
}
function sameChars(firstStr, secondStr)
{
var first = firstStr.split('').sort().join('');
var second = secondStr.split('').sort().join('');
return first.localeCompare(second)==0;
}
<input type="text" maxlength="512" id="input_1"/>
<input type="text" maxlength="512" id="input_2"/>
<button onclick="myFunction()">Check If Shuffled</button>
<p id="demo"></p>
Here's a modified version of Gurvinders answer.
var str1 = "hello",
str2 = "ehlol";
// Add sort on prototype of String object
String.prototype.sort = function () {
return this.split('').sort().join('');
};
// First check if length of both is same
var same = str1.length === str2.length && str1.sort() === str2.sort();
console.log('Strings are same?', same);
You could possibly say this:
(a.length === b.length) && (a.split('').every(function(val) { return b.indexOf(val) > -1}))
And, in ES6 you could make it look as follows:
(a.length === b.length) && a.split('').every(val => { return b.indexOf(val) > -1 })
Hello, I'm trying to make a simple matching game in javascript.
If the the user inserts the text president goes crazy in any way that contains every strings in word_tmp, then the word_tmp becomes true, and if he misses one string then it becomes false.
word_tmp = ['president', 'goes', 'crazy'];
// string 1 contains the president, goes and crazy at one string
string1 = 'president goes very crazy'; // should output true
// string 2 doesn't contain president so its false.
string2 = 'other people goes crazy'; // should output false
How can I accomplish this?
Try this:
var word_tmp = ['president', 'goes', 'crazy'];
var string1 = 'president goes very crazy';
var isMatch = true;
for(var i = 0; i < word_tmp.length; i++){
if (string1.indexOf(word_tmp[i]) == -1){
isMatch = false;
break;
}
}
return isMatch //will be true in this case
You can do it with simple reduce call:
word_tmp.reduce(function(res, pattern) {
return res && string1.indexOf(pattern) > -1;
}, true);
The same code, wrapped in a function:
var match_all = function(str, arr) {
return arr.reduce(function(res, pattern) {
return res && str.indexOf(pattern) > -1;
}, true);
};
match_all(string1, word_tmp); // true
match_all(string2, word_tmp); // false
But this solution won't work for you if you want to match whole words. I mean, it will accept strings like presidential elections goes crazy, because president is a part of the word presidential. If you want to eliminate such strings as well, you should split your original string first:
var match_all = function(str, arr) {
var parts = str.split(/\s/); // split on whitespaces
return arr.reduce(function(res, pattern) {
return res && parts.indexOf(pattern) > -1;
}, true);
};
match_all('presidential elections goes crazy', word_tmp); // false
In my example I'm splitting original string on whitespaces /\s/. If you allow punctuation marks then it's better to split on non-word characters /\W/.
var word_tmp = ['president', 'goes', 'crazy'];
var str = "president goes very crazy"
var origninaldata = str.split(" ")
var isMatch = false;
for(var i=0;i<word_tmp.length;i++) {
for(var j=0;j<origninaldata.length;j++) {
if(word_tmp[i]==origninaldata[j])
isMatch = true;
}
}
What's the easiest way to check to see if a number is in a comma delimited list?
console.log(provider[cardType]);
//returns: Object { name="visa", validLength="16,13", prefixRegExp=}
if (ccLength == 0 || (cardType > 0 && ccLength < provider[cardType].validLength)) {
triggerNotification('x', 'Your credit card number isn\'t long enough');
return false;
} else {
if ($('.credit-card input[name="cc_cvv"]').val().length < 3) {
triggerNotification('x', 'You must provide a CCV');
return false;
}
Seems similar to this SO question.
Just .split() the CSV and use inArray.
Not sure how your sample code relates to checking to see if a number is in a comma delimited list...
Also not sure if this is the easiest way, but it's what springs to mind:
<script type="text/javascript">
var myNumbers = "1,2,3,4,5";
var myArray = myNumbers.split( ',' );
// looking for "4"
for ( var i=0; i<myArray.length; i++ ) {
if (myArray[i] == 4) {
alert('Found it!');
break;
}
}
I do not see where you have a significant comma delimited list in the script you posted.
The fastest way could be something like
var csvList ="a,b,c,d,e,f,g,h";
var testList = ","+csvList+",";
var needle = "f";
alert(testList.indexOf(","+needle+",")!=-1)
just to be different ;)
If it's just a list of comma separated numbers with nothing fancy, you can just use the split method:
var numbers = list.split(",");
This will give you an array of all of the numbers in the list. Checking whether a number is in an array is trivial.
Native JavaScript and therefore cross-browser compliant. Some frameworks provide functions that do this for you, but you don't get more basic than the following.
var numbers = list.split(",");
var count = numbers.length;
var exists = false;
for (var i = 0; i < count; ++i) {
if (numbers[i] == anumber) {
exists = true;
break;
}
}
From your sample, I assume your question was "How do I see if a number is within a range of two values specified by a single-comma-delimited string?":
function inRange( number, stringRange ){
var minmax = stringRange.split(',');
minmax[0] = minmax[0]*1; //convert to number
minmax[1] = minmax[1]*1; //convert to number
minmax.sort(); // Ensure [0] is the min
return number>=minmax[0] && number<=minmax[1];
}
Try this one...
console.log(provider[cardType]); //returns: Object { name="visa", validLength="16,13", prefixRegExp=}
var regExp = new RegExp(",?" + ccLength + ",?");
if (ccLength == 0 || (cardType > 0 && !regExp.test(provider[cardType].validLength)))
{
triggerNotification('x', 'Your credit card number isn\'t long enough');
return false;
}
else
{
if ($('.credit-card input[name="cc_cvv"]').val().length < 3)
{
triggerNotification('x', 'You must provide a CCV');
return false;
}
}