SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search Text with Special Characters

    Hi,

    I am getting some problem to find out text position.
    I have textarea what have the following code.

    HTML Code:
    <textarea id="textId">This is Second Line.hello ' and "helo" & this is normal $it is £found '' and ; round , * ! look me
    <helo> ? no £4.00 is the price. "'" is the price which I am Looking and ; round , * !
    <helo> ? no £4.00 is the price. "'" and """ allow this is " cut
    hello ' and "helo" & this is normal $it is £pound '' and ; round , * ! yes sure
    <helo> ? no £4.00 is the price. "'" and """ allow this is " john
    </textarea>
    
    <input type="button" value="find" onclick="findText()" />

    here is the javascript

    Code:
    function findText(){
    var textarea=$("#textId").val();
    textarea=escape(textarea);
    textarea=unescape(textarea);
    
    var searchText="is the price. "'" and """ allow this is " cut
    hello ' and "helo" & this is normal $it is £pound";
    
    searchText=escape(searchText);
    searchText=unescape(searchText);
    
    var index=textarea.indexOf(searchText);
    alert(index);
    
    }
    Its always give me a -1 I also use the escape and unescape functions to try to search but result is same. Its could not find the position of text. Can any body help me in this matter.

  2. #2
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    You need to mask the double quotes which occur within the string you want to search for.
    You also need to use a newline character for the line break.

    This'll work:

    Code JavaScript:
    function findText(){
      var textarea=$("#textId").val();
      var searchText="is the price. \"'\" and \"\"\" allow this is \" cut\nhello ' and \"helo\" & this is normal $it is £pound";
      var index=textarea.indexOf(searchText);
      console.log(index);
    }

  3. #3
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    Hi,

    You need to mask the double quotes which occur within the string you want to search for.
    You also need to use a newline character for the line break.

    This'll work:

    Code JavaScript:
    function findText(){
      var textarea=$("#textId").val();
      var searchText="is the price. \"'\" and \"\"\" allow this is \" cut\nhello ' and \"helo\" & this is normal $it is £pound";
      var index=textarea.indexOf(searchText);
      console.log(index);
    }


    Thank you very much for your reply. Could you please tell me more that how to insert \ and \n in searchText because this values comes by user. Thanks once again.

  4. #4
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Hi,

    Sorry, I thought that the value which comes from the user is textarea.

    Taking that as an example:
    Imagine the user enters "Hello" (including the quotation marks).
    Using your method you would then test for that string like this:

    Code JavaScript:
    var textarea=$("#textId").val();
    var searchText="\"hello\"";
    var index=textarea.indexOf(searchText);
    console.log(index);

    The same for a newline.
    Imagine the user entered "hello" (without quotes), pressed return, then entered "hello" again.

    Code JavaScript:
    var textarea=$("#textId").val();
    var searchText="hello\nhello";
    var index=textarea.indexOf(searchText);
    console.log(index);

    If the location of the match is unimportant, you might consider:

    Code JavaScript:
    var pattern = /hello\nhello/,
         userInput = $("#textId").val();
    if (pattern.test(userInput)){
      console.log("Match!");
    }

    This way you don't have to worry about escaping quotes etc.

    Does that help, or have I understood what you are trying to do?

  5. #5
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Acutally the both values comes from user like textarea value and the search string.

  6. #6
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Quote Originally Posted by rashid100 View Post
    Acutally the both values comes from user like textarea value and the search string.
    Oh ok. Sorry for the misunderstanding.In that case, I'd probably do this:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Compare text areas</title>
      </head>
      <body>
        <textarea id="textArea1"></textarea>
        <textarea id="textArea2"></textarea>
        <input type="button" id="find" value="Find Match" />
        
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
        $("#find").on("click", function(){
          var val1 = $("#textArea1").val(),
              val2 = $("#textArea2").val();
          
          if (val1.match(val2)){
            console.log("Match!");
          }
        });
        </script>
      </body>
    </html>

  7. #7
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks once again for the reply. I test it but it gives me the error on console and also how I can find the position of text in textarea box?

  8. #8
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    This works for me in the latest Chrome:

    HTML Code:
    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Compare text areas</title>
        <style>textarea{ width:250px; height:100px;}</style>
      </head>
      <body>
        <textarea id="textArea1"></textarea>
        <textarea id="textArea2"></textarea>
        <input type="button" id="find" value="Find Match" />
        
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
        <script>
        $("#find").on("click", function(){
          var val1 = $("#textArea1").val(),
              val2 = $("#textArea2").val();
    
    	  console.log(val1.indexOf(val2));
        });
        </script>
      </body>
    </html>
    What is the error you are seeing?

  9. #9
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    console is undefined Thats the error

  10. #10
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    Ah ok, which browser are you using?

    Just change console.log() into alert()

  11. #11
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot

  12. #12
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    9 Thread(s)
    No problem

    If you want to find out about using the console and why it is good, here's a brief tutorial I wrote.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •