|
|
How to split on a backslash '\' ?
The following code does not seem to work, I have tried all
combinations of \\, \, \\\ etc. Unfortunately I can not change str .
<script type="text/javascript">
var str = 'Hello \ World';
var pattern = /\\/;
result = str.split( pattern );
alert(result[0]);
</script>
|
|
0
|
|
|
|
Reply
|
oprah.chopra (2)
|
3/8/2008 5:40:55 AM |
|
oprah.chopra@gmail.com said:
>
>The following code does not seem to work, I have tried all
>combinations of \\, \, \\\ etc. Unfortunately I can not change str .
>
><script type="text/javascript">
>
>var str = 'Hello \ World';
>var pattern = /\\/;
>result = str.split( pattern );
>
>alert(result[0]);
>
></script>
alert the value of str.
You'll be surprised.
--
|
|
0
|
|
|
|
Reply
|
Lee
|
3/8/2008 5:54:30 AM
|
|
oprah.chopra@gmail.com a �crit :
> The following code does not seem to work, I have tried all
> combinations of \\, \, \\\ etc. Unfortunately I can not change str .
>
> <script type="text/javascript">
>
> var str = 'Hello \ World';
> var pattern = /\\/;
the right patern would have to be :
var pattern = /\ /;
because :
var str = 'Hello \ World';
^^
> result = str.split( pattern );
or :
result = str.split( '\ ' );
> alert(result[0]);
>
> </script>
|
|
0
|
|
|
|
Reply
|
SAM
|
3/8/2008 10:53:09 AM
|
|
oprah.chopra@gmail.com wrote:
> The following code does not seem to work, I have tried all
> combinations of \\, \, \\\ etc. Unfortunately I can not change str .
Yes, you can, and you will have to.
> var str = 'Hello \ World';
Try window.alert(str), console.log(str) or print(str) after that, and you'll
see that it equals
var str = 'Hello World';
because "\ " is an unspecified string literal (like "\/" in the
common escape sequence "<\/" for "</" is). You are looking for
var str = 'Hello \\ World';
If this code is generated by ASP JScript, you can use
var str = '<%= foo.replace(/["'\\]/g, "\\$&") %>';
In PHP:
var str = '<?php echo addslashes($foo); ?>';
or with asp_tags=1:
var str = '<%= addslashes($foo); %>';
Perl:
$foo = 'Hello \\\'" World';
$foo =~ s/["'\\]/\\$&/g;
print <<EOD;
var str = '$foo';
EOD
Python (indent accordingly):
import re
foo = r'Hello \'" World'
print """\
var str = '%s';\
""" % re.sub(r'(["\'\\])', r'\\\1', foo)
Bourne Shell-compatible shell scripting with GNU textutils (provide sed(1)):
foo="Hello \\'\" World"
foo=$(echo "$foo" | sed 's/[\\"'\'']/\\\0/g')
echo "\
var str = '$foo'\
"
> var pattern = /\\/;
Unnecessary, you can pass the RegExp literal as-is.
> result = str.split( pattern );
There is no backslash in the parsed string, so no match that could be used
for splitting.
> alert(result[0]);
window.alert(result[0]);
PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
|
|
0
|
|
|
|
Reply
|
Thomas
|
3/8/2008 12:42:52 PM
|
|
|
3 Replies
682 Views
(page loaded in 0.153 seconds)
|
|
|
|
|
|
|
|
|