lifestyle
03-14-2011, 07:44 PM
I'm retreiving data from a texfile with AJAX through:
document.getElementById("my div").innerHTML=xmlhttp.responseText;
but I want to make a linebreak in the text before each capital letter starts. How do I go about doing that? Appreciate the help.
Could be a Regular Expression:
var text=xmlhttp.responseText.replace(/([\s])([A-Z])/g,'<br>$2');
document.getElementById("mydiv").innerHTML=text;
lifestyle
03-15-2011, 02:13 PM
Thank you - is it too much to ask for an explanation of
(/([\s])([A-Z])/g,'<br>$2');
?
Do you know Regular Expressions in JavaScript? If not, you should learn a little bit, before understanding the explanations:
http://lawrence.ecorp.net/inet/samples/regexp-intro.php
\s = a RegExp character matches a non-white-space
[...] = matches "positively" a set
(...) = groups the patterns in subsets
[A-Z] = the set of the capital letters, from A to Z (English alphabet) is the ASCII order. A range.
g = this flag makes the matches global (all the occurrences)
$1, $2... $99 = refer the defined subsets (...) in their written order
So:
- ([\s]) matches a non-white-space in a set.
- ([A-Z]) positioned immediately before a capital letter
- g all over the string
- keeps the second subset $2 (that means the Capital letters)
- but replaces the first subset (the non-white-space) with a certain substring. A tag <br> in this case.
The method is: string.replace(what,withWhat)
Example:
"Lorem Ipsum aedilit Pure"
becomes:
"Lorem<br>Ipsum aedilit<br>Pure"
lifestyle
03-15-2011, 09:09 PM
I can also use (/([A-Z])/g, "<br />$1"); right?
But why is it $1 and not $2 here?
devnull69
03-15-2011, 09:21 PM
$1 and $2 are placeholders for pattern matches in so called "grouping brackets ()"
So the $1 matches the [A-Z] in your example. $2 matches the same in Kor's example. In his example $1 would match [\s]
I can also use (/([A-Z])/g, "<br />$1"); right?
But why is it $1 and not $2 here?
Because there is no $2 there. You have only a single subset defined. ([A-Z])
It depends on what you want, precisely.
But that will insert a <br> before all the capital letters within the string. Including the first one.
Use something like that to test the RegExp solutions:
<script type="text/javascript">
var x='LoRem Ipsum aedilit Pure'
x=x.replace(/([A-Z])/g,'<br>$1')
alert(x)
</script>
<script type="text/javascript">
var x='Lorem Ipsum aedilit Pure'
x=x.replace(/([\s])([A-Z])/g,'<br>$2')
alert(x)
</script>
$1 and $2 are placeholders for pattern matches in so called "grouping brackets ()"
Yes. That is their meaning. The limit is $99. But match is not quite the proper word. The $ token does not return anything. It points something. A sort of word like placefinger is, maybe, a better one. :)
lifestyle
03-15-2011, 09:46 PM
Ah ok, then I'll go with (/([A-Z])/g,'<br>$1') cause I need a new line before every capital letter. Thanks alot for your help!
jonewatson
03-23-2011, 01:25 PM
As you want to make a linebreak in the text before each capital letter starts. So you should go through such a code:
var text=xmlhttp.responseText.replace(/([\s])([A-Z])/g,'<br>$2');
document.getElementById("mydiv").innerHTML=text;