Solved

How to find a string in a variable field using javascript

Posted on 2016-07-22
8
46 Views
Last Modified: 2016-07-23
Hello,   I am new to javascript and been searching for a way to search for a string to be found in a variable field.  
Example:  
Have a variable x    x="Hello World"   I would like to check the variable x to see if it contains "He".  
I thought I could use something like this:
if (trimTrailingSpaces(x)=="He*") {     } .
.
Is there a  way to do this using javascripts.
Like I said I am new to this.  
Any help would be appreciated.
Thanks
0
Comment
Question by:tambrosi
8 Comments
 
LVL 5

Assisted Solution

by:Eric C
Eric C earned 125 total points
Comment Utility
EXAMPLE:

var myList = "Ford, Chevy, and Toyota";
var mySearch = "Audi";

// function
function findInList (vehicle) {
    if (myList.indexOf(vehicle) >= 0) {
        console.log(vehicle + " is in the string");
    }
    else {
        console.log(vehicle + " is not in the string.");
    }
}


// call the function
findInList(mySearch);
0
 
LVL 15

Assisted Solution

by:gurpsbassi
gurpsbassi earned 125 total points
Comment Utility
if(x.indexOf("He") > -1){
  //success
}
0
 
LVL 5

Expert Comment

by:Eric C
Comment Utility
My comment is the same result ... > -1 is the same as >= 0.     ;-)
0
 
LVL 15

Expert Comment

by:gurpsbassi
Comment Utility
My comment is the same result ... > -1 is the same as >= 0.     ;-)

yes depends on coding style.
both are valid.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Posting JavaScript questions in the Java TA is not appropriate, and might make your question less likely to be answered. http://technojeeves.com/joomla/index.php/free/127-javascript-is-not-java
2
 
LVL 21

Expert Comment

by:Kim Walker
Comment Utility
if (trimTrailingSpaces(x)=="He*") {     }
If you were using a search method that recognizes the asterisk as a wild card character, the above condition would be looking for a string that begins with "He", not just contains "He". Which do you wish to search for, contains or begins with?
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
Comment Utility
Given your "He*" example, I am going to assume you like to think in pattern matching style.

In Javascript one would use a regular expression to achieve this:

if (x.match(/He/)) { .... }

Open in new window


If you want to make sure the variable starts with "He", you would need to add a begin of line marker:
if(x.match(/^He/)) { ... }

Open in new window


You can play with regular expressions at various sites. I just looked up this one on a search engine: regexr.com

Kind regards,

-r-
0
 

Author Closing Comment

by:tambrosi
Comment Utility
First, sorry I posted question to wrong area.
Thanks for all of the answers, all of them posted really helped me understand the javascripting code stream.  That was the first time I looked/tested the function process, and it will be something I can use in the future.
Like I said, I am really new to this and really appreciated the help!!
Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now