Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Javascript] Replace Characters
- From: liorean at gmail.com (liorean)
- Subject: [Javascript] Replace Characters
- Date: Wed May 16 22:29:27 2007
On 16/05/07, Tim Lewis <twlewis@xxxxxxxxxxxx> wrote:
> I have a file with carriage returns(ASCII 13) only, and I need to replace
> these with carriage return and line feed(ASCII 13 and 10).
>
> My code was not work, so I created a simple test using the
> String.fromCharCode and replace. It is not working either. I have
> included the simple code below. The idea in the code is to replace the d
> in dog with an l. It appears that the replace function does not recognize
> an ASCII value. Is there a way to make the replace function use an ASCII
> value?
Yes there is, but not through doing this:
> sLineOut = sLineIn.replace(/chr100/g,chr108);
You are here trying to replace any piece of the string that contains
one of each of the characters 'c', 'h', 'r', '1', '0' and '0', in that
order, with the value from the variable chr108.
You can't enter variables into regex, short of by using
new RegExp(...)
However, you can use the same character escapes in regex as you can in
JavaScript strings. CR is '\u000d', '\x0d', or '\r'. LF is '\u000a',
'\x0a' or '\n'. Note however that some browsers use '\n' in strings
for the platform convention line ending and not for LF, so in this
case I'd recommend you to use the unicode literal.
--
David "liorean" Andersson
- Follow-Ups:
- [Javascript] Replace Characters
- From: Tim Lewis
- [Javascript] Replace Characters
- References:
- [Javascript] Replace Characters
- From: Tim Lewis
- [Javascript] Replace Characters
- Prev by Date: [Javascript] Passing parameters to a new page
- Next by Date: [Javascript] Replace Characters
- Previous by thread: [Javascript] Replace Characters
- Next by thread: [Javascript] Replace Characters
- Index(es):