Javascript and those naughty characters...
Ah, yea....
Anyways, I'm trying to strip our bad characters from a block of text with javascript. This seems to work sometimes but not others and I think it has to do with me not stripping out all the bad characters.
Of course, I could be completely wrong.
Here is my code so far, which truncates the resulting text:
<script type="text/JavaScript">
var outputstr = "!---TEXT---"
var textpreview = outputstr.replace(/[^a-zA-Z 0-9]+/g,'')
if (textpreview.length > 50)
{
document.write(textpreview.substr(0,50) + " ...")
}
else
{
document.write(textpreview)
}
</script>
The !---TEXT--- is a text file created by our shopping cart and holds product information. I thought I was only returning A-Z and 0-9 with my regex but I'm not sure if I'm doing it correctly.
Now, I said it work sometimes and not with others. The major difference I see with the ones that work and the ones that don't is that there are quotes "" in the text or there may be some html in the text.
A. Would this cause the javascript to break?
B. If so, what can I do?
C. Am I completely wrong about why it's not working?
D. Just give up this javascript stuff because I don't know what the hell I'm doing.
Thanks in advance,
DaK
|