0
  1. user enters 2 strings
  2. use a regular expression to search for the second string within the first string.

Those are my directions to follow. I was not told what to do with the search results so I decided to use the exec() method to display the results.

jsfiddle code example

My code does not display any results.

html:

<div id="string_search">
    <h3>Requirement #1</h3>
    <p>Enter a string in the first box. <br>
    Enter a search term in the second box.<br>
    Click the button to find the term.</p>

    <textarea id="user_string" rows="4" cols="50" placeholder="Enter string (text) here."></textarea>
    <form>
    Enter Search Term:<input type="text" id="search_term" size="45"><br>
    <input type="button" id="search_button" value="Search" onclick="SearchString()"><br>
    </form>
    <textarea id="search_results" placeholder="Results here..." rows="4" cols="30"></textarea>
</div>

javascript:

function SearchString(){
    var user_string = document.getElementById("user_string").innerHTML;
    var search_term = document.getElementById("search_term").value;
    // regex to find all instances of search term
    var re = new RegExp(search_term,"gi");
    //use exec() to return matched text
    var result = re.exec(user_string);
    //display results in textarea "search_results"
    document.getElementById("search_results").innerHTML = result;
}

I am not getting console errors, is my code not functional? maybe there's a spelling error im not seeing or similar. Been staring at this for awhile,

1 Answer 1

3

Works fine, you had some mistakes.

http://jsfiddle.net/6p3odux1/

function SearchString(){
    var user_string = document.getElementById("user_string").value;
    var search_term = document.getElementById("search_term").value;
    // regex to find all instances of search term
    var re = new RegExp(search_term,"gi");
    //use exec() to return matched text
    var result = re.exec(user_string);
    //display results in textarea "search_results"
    document.getElementById("search_results").innerHTML = result;
}

any input element you should use value not innerHTML

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.