I'm trying to find some text in a long string but my code does not work, For Example:
Var result = “<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN html><head<title>Hey i am here</title>”
if (result.search('Hey i am here')== true) {
alert('found');
} else { alert('NOT found'); }
But This dont Works :(
Please help
var is lower case
Strings can be delimited with " or ' but not with “, don't use curly quotes.
The search method expects a regular expression (although it will try to convert a string if it gets one)
If you want a simple string match, then indexOf is more efficient then search.
Both search and indexOf return the index of the first match (or -1 if it doesn't find one), not a boolean.
As an aside, that Doctype will trigger quirks mode, so never use it in a real HTML document.
var result = "<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN html><head<title>Hey i am here</title>"
if (result.indexOf("Hey i am here") >= 0) {
alert('found');
} else {
alert('NOT found');
}
I thinks you need to use an .indexOf function.
so your if statement would be
if (results.indexOf('Hey i am here') != -1) {
alert('found');
} else { alert('NOT found'); }
There are a lot of ways to do this:
See here
Search method returns the position of the match, or -1 if no match is found. Not true or false.
Related
If i am giving Special Symbol only at beginning then it's working otherwise it's not working.
For example:
var password = '#Sourav12345'
if (password.search(/[#_!#$%^&*()<>?/\|}{~:]/)) {
return true
}
else{
return false
}
If i will change password to Sourav#12345.it won't work .Plz. help me
Your regex should work perfectly fine, the issue you are probably encountering is that search() returns the index if the first matched occurencens found, otherwise -1.
So only your case where # is the first character will evalute to false in your condition. You would need to adapt your condition:
var password = 'Sourav12345#.it'
var search = password.search(/[#_!#$%^&*()<>?/\|}{~:]/);
if (search >= 0) {
console.log(true)
}
else{
console.log(false)
}
Or use a different mehtod to check against a regex like test()
var password = 'Sourav12345#.it'
var test = /[#_!#$%^&*()<>?/\|}{~:]/.test(password);
if (test) {
console.log(true)
} else {
console.log(false)
}
This is the correct regex:
/[#_!#$%^&*()<>?\/\|}{~:]/
Just escaped the "/" to make it work
The search() method searches a string for a specified value and returns the position of the match.
if it is not found it will return -1 else it returns the position number
var x='#Sourav12345'.search('/[#_!#$%^&*()<>?/\|}{~:]/') > 0?true:false;
console.log(x)
I need to flag a textarea that contains a URL starting with http://, but not with https://. I thought this should work, but I'm getting the alert even when all URLs are https.
$('#template_form').submit(function() {
alert("this is the text: " + $("#template_data").val() );
val = $("#template_data").val();
if (val.search(/^http:\/\//)){
alert("there's a URL in there...");
return false;
}
return true;
});
<textarea id="template_data">This is a test of the new URL validation. Let's add a link to https://www.test.com</textarea>
This should only present the second alert if the URL were http://www.test.com, but it's throwing it even as is, with https://. What am I doing wrong?
From the documentation for search():
A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
-1 will make the if statement evaluate to true (if (-1) {alert("true");}. So either switch to match() or test(), or check for if (val.search(...) > -1)
Also the ^ is wrong in your regex, it would only match from the start of the string.
$('#template_form').submit(function() {
alert("this is the text: " + $("#template_data").val());
val = $("#template_data").val();
if (val.match(/http:\/\//)) {
alert("there's a URL in there...");
return false;
}
return true;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="template_form">
<textarea id="template_data">This is a test of the new URL validation. Let's add a link to https://www.test.com</textarea>
<input type="submit" value="submit">
</form>
String.search() is not boolean:
Return value
The index of the first match between the regular expression and the given string; if not found, -1.
Further in that same piece of documentation:
When you want to know whether a pattern is found and also its index in
a string use search() (if you only want to know it exists, use the
similar test() method, which returns a boolean)
$('#template_form').submit(function() {
if ($("#template_data").val().indexOf("http:\/\/") > -1) {
return false;
}
return true;
});
Here is another way.
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.
I'm trying to check for two different hash substrings and execute different code depending. This is what I have right now:
Javascript
if(window.location.hash.substring(confirm) {
$('.confirm').click();
}
elseif(window.location.hash.substring(thanks) {
$('.thanks').click();
}
Any idea what I am doing wrong?
Use indexOf with quotes to denote the string you want to search:
if(window.location.hash.indexOf('confirm') >= 0) {
$('.confirm').click();
}
else if(window.location.hash.indexOf('thanks') >= 0) {
$('.thanks').click();
}
BTW, in your original implementation, you were also missing ) in the if condition.
I have a problem using quotation marks in js...
I have an input field using this js-function
function validate(xyz) {
"+umum+" == "yeah_it_is_ok";
if(xyz == ""+umum+"") {
alert("Hoera!");
return true;
} else {
alert("Too bad!");
return false;
}
}
What do I have to insert in the input-field to get the Hoera message?$
In other words, what is the function of a " or a + in js?
You don't have a syntax error in the function declaration,
but it will fail at execution time, because umum is not defined;
and surely you have a semantic error, because the only way to get "Hoera"
is to declare the umum var first and call the validate function later:
var umum;
validate("test value");
Of course, it always give a "too bad!" message unless you pass ""+undefined+""
as parameter. I think the right function should be:
function validate(xyz) {
var umum = "yeah_it_is_ok"; // or whatever you want to validate with..
if(xyz == umum) {
alert("Hoera!");
return true;
} else {
alert("Too bad!");
return false;
}
}
In this case, when calling validate("yeah_it_is_ok") you'll get an "Hoera!".
You would want to declare a variable like this:
var umum = "yeah_it_is_ok";
Note the var keyword and the use of a single equals for assignment.
Also, a pair of " characters is used to enclose a string variable, and the + will concatenate two strings. However, if you wish to have a double-quotation within a string you need to escape it with a backspace \. For example:
if(xyz == "\"+umum+\"") {
Single- and double-quote characters are used to delimit string constants. The + character is an operator that serves several purposes, including string concatenation, numeric addition, and asserting numeric "positiveness" (used often for its implicit side effects).
I think you mean to write your function like this.
function validate(xyz) {
umum = "yeah_it_is_ok";
if(xyz == umum) {
alert("Hoera!");
return true;
} else {
alert("Too bad!");
return false;
}
}
So then to answer your question, you can put the string that your looking for into the input-field. Which, since you don't have an input field in your example, we can just call the function with the correct string.
validate("yeah_it_is_ok");
Also it seems like you were thinking that you can use " or + in a variable. You can't do that. As others have suggested, you should learn the basics of JavaScript. w3schools.com and the Mozilla Developer Network are good places to do that.
http://www.w3schools.com/js/default.asp
https://developer.mozilla.org/en-US/learn/javascript
I believe you put a \ infront of it
so like
if(xyz == "\"+umum+\"") {