PDA

View Full Version : Isolate and edit a line of text.



Mhtml
03-29-2003, 10:05 AM
How can I isolate a line of text and edit it?

For example, the string -

This is a test string!
This is the second line of the string!
Oh and this is a third!

How can I add a symbol "*" at the begining and end of a line that has "O" as the first character?

So the string would be -

This is a test string!
This is the second line of the string!
*Oh and this is a third!*

ConfusedOfLife
03-29-2003, 12:01 PM
Ok, you asked 2 questions: How to isolate a line and how to edit it.

As for as isolation it really depends on how you're getting your contents. If you're getting it from a textarea ( that I assumed in my dummy progie ), you should split it by "\n" that is what Windows use for splitting lines. So, it's better you say what you are working on, that we can tell you how to isolate it!

And for editing a line, you have to use regular expressions.

In the example that you said, I wrote an ugly piece of code that gets the input from a textare, isolates it and edit the lines to add those asteriks at the beginning and end of each line that starts with O. It's probably a cleaner and better way if you used regular expressions, but well, I'm not doing it now!



<textarea id="Oopse" style="height: 200px; width: 500px;">
This is a test string!
This is the second line of the string!
Oh and this is a third!
</textarea>
<br /><br /><br />
<div id="testArea" style="background: #0099cc; color: white;">
&nbsp;
</div>

<!------------------------------------------------------------------->

<script>
strArray = document.getElementById("Oopse").value.split("\n");
temp = "";
for (i=0; i<strArray.length-1; i++)
{
temp += ( strArray[i].charAt(0).toLowerCase() == "o" ) ? ( "*" + strArray[i] + "*" ) : ( strArray[i] );
temp += "<br />";
}
document.getElementById("testArea").innerHTML = temp;
</script>

Mhtml
03-29-2003, 12:09 PM
Thanks! I was thinking of split but didn't think about using /n .. lol :D I think I can finish my script now.

Thanks again.. :)

ConfusedOfLife
03-29-2003, 12:30 PM
You're welcome!

If you use regular expressions in your script instead of my function, please write it in here too that I can see!