I am getting some problem to find out text position.
I have textarea what have the following 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
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.
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:
function findText(){
var textarea=$("#textId").val();
var searchText="is the price. \\"'\\" and \\"\\"\\" allow this is \\" cut\
hello ' 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
in searchText because this values comes by user. Thanks once again.
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:
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.
var textarea=$("#textId").val();
var searchText="hello\
hello";
var index=textarea.indexOf(searchText);
console.log(index);
If the location of the match is unimportant, you might consider:
var pattern = /hello\
hello/,
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?