Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete lines from textarea? 2

Status
Not open for further replies.

mwpc

Programmer
Apr 28, 2002
53
US
Working in a text area, how do I delete lines NOT beginning with http?

For example, so that

http:index2.html
http:
will become

http:http:
How do I remove duplicates?

so that

http:http:http:http:
will become

http:http:
 
Here is a solution to what I believe you need. A regular expression is used to obtain only wellformed urls and from there the dups are checked for as the new output is built.

<html>
<head><title>Test TextArea</title><head>
<body>
<form name=&quot;example&quot;>
<table border=&quot;0&quot;>
<tr>
<td>
<textarea name=&quot;myTextArea&quot; rows=&quot;4&quot; cols=&quot;25&quot;> hello asdsd http:';' </td>
</tr>
</table>
</form>
<body>
</html>
<script language=&quot;javascript&quot;>
var myForm = window.document.forms[0];
var text = myForm.myTextArea.value;
var pattern = /http:\/\/([a-zA-Z0-9_.]+)/gi;
var myArray = text.match(pattern);
var outtext = &quot;&quot;;
var firstAdded = false;
for (i=0;i<myArray.length;i++)
{
if (outtext.indexOf(myArray) == -1)
{
outtext = outtext + (firstAdded?&quot; &quot; : &quot;&quot;) + myArray;
firstAdded = true;
}
}
myForm.myTextArea.value = outtext;
</script>
 
pbarry30, thank you for your comments. I think it is close.

My objective is to remove lines from a textarea which do NOT begin with http.

Here is the url where test scripts are running


Copies of the backend cgi scripting may be viewed at




I am grateful for any suggestions!
 
Code:
function Htt(){
t=document.frm.tex.value;
ta=t.split(&quot;\n&quot;);
for(i=0;i<ta.length;i++){
if(ta[i].substring(0,4)!=&quot;http&quot;){
ta[i]=&quot;&quot;;
//maybe ta[i]=null; would work for the above line, too
}
}
tz=ta.join(&quot;&quot;);
document.frm.tex.value=tz;
}
where the form has been named 'frm' and the textarea has
been named 'tex'
It might also be easier to remove the unwanted lines with the perl script, as in
@tex=split(&quot;\n&quot;,$textvalue);
foreach(@tex){
if(substr($_,0,4) ne &quot;http&quot;){
$_=&quot;&quot;;
}
}
$answer=join('&quot;,@tex);

 
Thanks to leehinkleman, it is now working at


There is one issue that may not have a solution -- highlight & copy to clipboard works in IE but only highlights in netscape -- won't copy to clipboard.

<script language=&quot;Javascript&quot;>
<!--

var copytoclip=1

function HighlightAll(theField) {
var tempval=eval(&quot;document.&quot;+theField)
tempval.focus()
tempval.select()
if (document.all&&copytoclip==1){
therange=tempval.createTextRange()
therange.execCommand(&quot;Copy&quot;)
window.status=&quot;Contents highlighted and copied to clipboard!&quot;
setTimeout(&quot;window.status=''&quot;,1800)
}
}
//-->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top