Answer #Wiktor Stribiżew suggested:
function myValidate(word) {
return (word.length === 1 || /[^A-Z]/i.test(word)) ? true : false;
}
Hello during the creation of an array I have a function that will not allow words with certain characters etc to be added to the array
function myValidate(word) {
// No one letter words
if (word.length === 1) {
return true;
}
if (word.indexOf('^') > -1 || word.indexOf('$') > -1) {
return true;
}
return false;
}
It seems like not the proper way of going about this and ive been looking into a regex that would handle it but have not been successful implementing it, tried numerous efforts like:
if (word.match('/[^A-Za-z]+/g') ) {
return true;
}
can some one shed some light on the proper way of handling this?
I suggest using a simpler solution:
function myValidate(word) {
return (word.length === 1 || /[^A-Z]/i.test(word)) ? false : true;
}
var words = ["Fat", "Gnat", "x3-2741996", "1996", "user[50]", "definitions(edit)", "synopsis)"];
document.body.innerHTML = JSON.stringify(words.filter(x => myValidate(x)));
Where:
word.length === 1 checks for the string length
/[^A-Z]/i.test(word) checks if there is a non-ASCII-letter symbol in the string
If any of the above condition is met, the word is taken out of the array. The rest remains.
EDIT: using test instead of match
You want to use test() because it returns a bool telling you if you match the regex or not. The match(), instead, always returns the matched elements. Those may be cast to true by coercion. This is not what you want.
To sum it all up you can just use this one-liner (no if needed and no quotes either, cannot get any simpler):
return word.test(/^[a-zA-Z][a-zA-Z]+$/); // two letter words
You should whitelist characters instead of blacklisting. That's one of the principles in security. In your case, don't tell what is wrong, but tell what is right:
if (word.test('/^[a-zA-Z]+$/')) { // two letter words
return false;
}
This will return false for all words that contain ONLY [a-zA-Z] characters. I guess this is what you want.
Your regex, instead, looked for illegal characters by negating the character group with the leading ^.
Two recommendations:
Just use regex in a positive way (without negation) and it'll be a lot easier to understand.
Also, validation functions normally return true for good data and false for bad data.
It is more readable this way:
if (validate(data))
{
// that's some good data we have here!
}
Related
I have a function to validate phone number in a contact form, but i need to be able to put in "xxx xxx xxxx" for example, and not just "xxxxxxxx"
The number format should be:
xxx xxx xxxx
xxx-xxx-xxxx
xxx.xxx.xxxx
function validatePhone() {
var phone = document.getElementById("phone").value;
if (phone.length == 0) {
var w = document.getElementById("phoneError").textContent;
alert(w);
return false;
}
if (phone.length != 10) {
var r = document.getElementById("phoneError").textContent;
alert(r);
return false;
}
// THIS IS NOT WORKING
if (
!phone.match(/^[0-9]{10}$/) ||
!phone.match(/^\d{3}-\d{3}-\d{4}$/) ||
!phone.match(/^\d{3}.\d{3}.\d{4}$/)
) {
var t = document.getElementById("phoneError").textContent;
alert(t);
return false;
}
}
Two things: First, you are mixing up AND and OR:
if (
!phone.match(/^[0-9]{10}$/) ||
!phone.match(/^\d{3}-\d{3}-\d{4}$/) ||
!phone.match(/^\d{3}.\d{3}.\d{4}$/)
) {
As soon as one of the conditions fails, it will return false (which is basically always). You want this if to apply, when none of the expressions matches, e.g. when all of them are false. Therefor, you have to use && instead of ||. Not a AND not b AND not c.
Second: your 3rd regex is a bit off: . means "any character", so this regex would also match "123x123y1234". You need to escape the dot with a backslash: /^\d{3}\.\d{3}\.\d{4}$/
Also, you can improve this code significantly. You have 5 conditions, which could all be handled in one (if you want to allow the input of "123.123 234", otherwise you will have to do it using 3 regex). And for just checking if a regex matches a string, you maybe should use test(), because it is just slightly faster (it won't matter in your case, but just out of principle).
You can reduce your code to:
if (/^\d{3}[\s-.]\d{3}[\s-.]\d{4}$/.test(document.getElementById("phone").value) === false) {
alert (document.getElementById("phoneError").textContent);
return false;
}
I am sure there is probably a dupe of this here somewhere, but if so I cannot seem to find it, nor can I glue the pieces together correctly from what I could find to get what I need. I am using JavaScript and need the following:
1) Replace the first character of a string with it's Unicode aware capitalization UNLESS the next (second) character is a - OR ` or ' (minus/dash, caret, or single-quote).
I have come close with what I could find except for getting the caret and single quote included (assuming they need to be escaped somehow) and what I believe to be a scope issue with the following because first returns undefined. I am also not positive which JS/String functions are Unicode aware:
autoCorrect = (str) => {
return str.replace(/^./, function(first) {
// if next char is not - OR ` OR ' <- not sure how to handle caret and quote
if(str.charAt(1) != '-' ) {
return first.toUpperCase(); // first is undefined here - scope??
}
});
}
Any help is appreciated!
Internally, JavaScript uses UCS-2, not UTF-8.
Handling Unicode in JavaScript isn't particularly beautiful, but possible. It becomes particularly ugly with surrogate pairs such as "🐱", but the for..of loop can handle that. Do never try to use indices on Unicode strings, as you might get only one half of a surrogate pair (which breaks Unicode).
This should handle Unicode well and do what you want:
function autoCorrect(string) {
let i = 0, firstSymbol;
const blacklist = ["-", "`", "'"];
for (const symbol of string) {
if (i === 0) {
firstSymbol = symbol;
}
else if (i === 1 && blacklist.some(char => char === symbol)) {
return string;
}
else {
const rest = string.substring(firstSymbol.length);
return firstSymbol.toUpperCase() + rest;
}
++i;
}
return string.toUpperCase();
}
Tests
console.assert(autoCorrect("δα") === "Δα");
console.assert(autoCorrect("🐱") === "🐱");
console.assert(autoCorrect("d") === "D");
console.assert(autoCorrect("t-minus-one") === "t-minus-one");
console.assert(autoCorrect("t`minus`one") === "t`minus`one");
console.assert(autoCorrect("t'minus'one") === "t'minus'one");
console.assert(autoCorrect("t^minus^one") === "T^minus^one");
console.assert(autoCorrect("t_minus_one") === "T_minus_one");
A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'". - wikipedia
Our goal is to determine whether or not a given string is a valid palindrome or not.
Test cases:
Test.assertEquals(palindrome("Amore, Roma"), true)
Test.assertEquals(palindrome("A man, a plan, a canal: Panama"), true)
Test.assertEquals(palindrome("No 'x' in 'Nixon'"), true)
Test.assertEquals(palindrome("Abba Zabba, you're my only friend"), false)
My code so far:
function palindrome(string) {
var str = string.toLowerCase().replace(/[^a-z]+/g,"");
var rev= str.split("").reverse().join("");
if (string == rev) {
return true;
} else {
return false;
}
}
Apparently join is undefined but I don't understand why?
I tried your examples with the following changes and it works on OSX 10.9:
function palindrome(string) {
var str = string.toLowerCase().replace(/[^a-z]/g, "");
var rev = str.split("").reverse().join("");
return (str == rev);
}
It appears the array join() method has been part of Javascript since version 1.1 -- both the specific error message and some description of your environment should help resolve this.
The problem:
Have the function SimpleSymbols(str) take the str parameter being passed and determine if it is an acceptable sequence by either returning the string true or false. The str parameter will be composed of + and = symbols with several letters between them (ie. ++d+===+c++==a) and for the string to be true each letter must be surrounded by a + symbol. So the string to the left would be false. The string will not be empty and will have at least one letter.
My code:
function SimpleSymbols(str) {
var arr = str.match(/[\+][a-zA-Z][\+]/g);
var total = str.match(/[a-zA-Z]/g);
if(arr === null || total === null)
return false;
else if(arr.length >= 1 && arr.length === total.length)
return true;
else
return false;
}
All the test cases except for these three pass:
-"+z+z+z+"
-"2+a+a+"
-"+z+z+==+a+"
What I've done: checked the other question on this problem. Tried another solution using regex but it had issues with input like "b".
I think the problem has something to do with when the pattern is "+char+char+" since a lot of the other test cases are like "++char+==+char+=="
this might be a very basic question, but I would like to know how I can find out if .html() has a particular value (in this case a string). An example:
<p id="text">Hello this is a long text with numbers like 01234567</p>
and I would like to ask
var $text = $('#text');
if ($text.html() == '01234567')
of course this would not work. But how can I enhance another method to .html() that asks
if($text.html().contains() == '01234567');
Important to say is, that in my case I definitely will search for things who are seperated with a space, not like withnumberslike01234567 but indeed it would be interesting if that would work as well.
Thanks in advance!
(' ' + document.getElementById('text').textContent + ' ').indexOf(' 01234567 ') != -1
Fixes problem with the text at the beginning, doesn't abuse regex, and hooray for vanilla.js!
You can use indexOf:
var text = $('#text').html();
if(text.indexOf(" 01234567") != -1) {
// your logic
}
Your HTML might start with 01234567, though; in that case, you can do this:
if((' ' + text).indexOf(" 01234567") != -1) {
// your logic
}
Thanks, bjb568 and Felix Kling.
As I understand from OP, these are the test cases:
hello12348hello // false
hello 1234hello // false
hello012348 hello // false
hello 1234 hello // TRUE
1234hello // false
hello1234 // false
1234 hello // TRUE
hello 1234 // TRUE
// false
1234 // TRUE
1234 // TRUE
** Changing "" by any other white-space character (e.g. \t, \n, ...) should give same results.
As OP said:
for things who are separated with a space, not like withnumberslike01234567
So, hello 01234567withnumberslike is also wrong!!!
Creating the function:
function contains(value, searchString){
// option 1: splitting and finding a word separated by white spaces
var words = value.split(/\s+/g);
for (var i = 0; i < words.length; i++){
if (words[i] === searchString){
return true;
}
}
return false;
// option 1a: for IE9+
return value.split(/\s+/g).indexOf(searchString) > -1;
// option 2: using RegEx
return (new RegExp("\\b" + searchString + "\\b")).test(value);
return (new RegExp("(^|\\s)" + searchString + "($|\\s)")).test(value); // this also works
// option 3: Hardcoded RegEx
return /\b1234\b/.test(value);
}
See case tests here in jsFiddle
It will also accept tabs as well as whitespaces..
NOTE I wouldn't worry about using RegEx, it isn't fast as indexOf, but it stills really fast. It shouldn't be an issue, unless you iterate millions of times. If it would be the case, perhaps you'll need to rethink your approach because probably something is wrong..
I would say to you think about compatibility, there is a lot of users still using IE8, IE7, even IE6 (almost 10% right now - April, 2014). -- No longer an issue in 2016..
Also, it's preferred to maintain code standards.
Since, you are using jQuery you can use too .text() to find string:
var element = $(this);
var elementText = element.text();
if (contains(elementText, "1234"){
element.text(elementText.replace("1234", "$ 1234.00"))
.addClass("matchedString");
$('#otherElement').text("matched: 1234");
}
Thanks to #Karl-AndréGagnon for the tips.
\b: any boundary word (or start/end of the string)
^: start of the string
\s: Any whitespace character
$: end of the string
http://rubular.com/r/Ul6Ci4pcCf
You can use the String.indexOf method in JavaScript to determine whether or not one string is contained in another. If the string passed into indexOf is not in the string, then -1 is returned. This is the behavior you should utilize.
If ($test.html().indexOf("1234567890") != -1)
//Do Something
if($text.html().indexOf('01234567') != -1) {
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf