How to find a string in a variable field using javascript

Posted on 2016-07-22
Medium Priority
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.  
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.
Question by:tambrosi

Assisted Solution

by:Eric C
Eric C earned 500 total points
ID: 41725213

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
LVL 16

Assisted Solution

gurpsbassi earned 500 total points
ID: 41725332
if(x.indexOf("He") > -1){

Expert Comment

by:Eric C
ID: 41725348
My comment is the same result ... > -1 is the same as >= 0.     ;-)
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

LVL 16

Expert Comment

ID: 41725357
My comment is the same result ... > -1 is the same as >= 0.     ;-)

yes depends on coding style.
both are valid.
LVL 86

Expert Comment

ID: 41725419
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
LVL 22

Expert Comment

by:Kim Walker
ID: 41725576
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?
LVL 49

Accepted Solution

Roonaan earned 1000 total points
ID: 41725585
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,


Author Closing Comment

ID: 41725759
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!!

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

627 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