473,513 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

simple regular expression but not work

den
if I have:
string=/AAA/B/file.jpg;

if I want change first slash, I writeo:
var regexp = /\//;
var_2 = stringa.replace(regexp, "zzz");

ok work and I have:
zzzAAA/B/file.jpg;

but if I want change only the last slash; this code not work:
var regexp = /$\//;

where is my error?
Feb 25 '07 #1
9 1260
den wrote:
if I have:
string=/AAA/B/file.jpg;

if I want change first slash, I writeo:
var regexp = /\//;
var_2 = stringa.replace(regexp, "zzz");

ok work and I have:
zzzAAA/B/file.jpg;

but if I want change only the last slash; this code not work:
var regexp = /$\//;

where is my error?
$ must come last and can't be followed by anything else. A simple way
to overcome your problem is to reverse the string, then execute your
initial regex and then reverse it back:

var string = '/AAA/B/file.jpg';
var r = '';
for (i = string.length - 1; i >= 0; i--)
r += string.charAt(i);
r = r.replace(/\//, 'zzz')
var string = '';
for (var j = r.length - 1; j >= 0; j--)
string += r.charAt(j);
alert(string);

Hope this helps,

--
Bart

Feb 25 '07 #2
den
Il 25 Feb 2007 01:32:38 -0800, Bart Van der Donck ha scritto:
den wrote:
>if I have:
string=/AAA/B/file.jpg;

if I want change first slash, I writeo:
var regexp = /\//;
var_2 = stringa.replace(regexp, "zzz");

ok work and I have:
zzzAAA/B/file.jpg;

but if I want change only the last slash; this code not work:
var regexp = /$\//;

where is my error?
if I use this :
/\/$/
why not work ?
I have insert the $ at the end
Feb 25 '07 #3
den wrote on 25 feb 2007 in comp.lang.javascript:
Il 25 Feb 2007 01:32:38 -0800, Bart Van der Donck ha scritto:
>den wrote:
>>if I have:
string=/AAA/B/file.jpg;
use quotes for a litteral string!

do not use the reserved word "string" for a variable,
but stringa as you reference below!!!
>>>
if I want change first slash, I writeo:
var regexp = /\//;
var_2 = stringa.replace(regexp, "zzz");

ok work and I have:
zzzAAA/B/file.jpg;

but if I want change only the last slash; this code not work:
var regexp = /$\//;

where is my error?

if I use this :
/\/$/
why not work ?
I have insert the $ at the end
No, that should only work if the $ is te last char.

Try:

<script type='text/javascript'>

s ='/AAA/B/file.jpg';

var regexp = /\/([^\/]*)$/;
var2 = s.replace(regexp, "zzz$1");

alert(var2);

</script>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Feb 25 '07 #4
:)

Evertjan has posted my same solution.. but i have added a little
explanation so there's my answer to the problem.
if I use this :
/\/$/
why not work ?
I have insert the $ at the end
As Bart is correctly saying '$' is not a 'last occurrence identifier'.
It is used to match the end of the line (while ^ is used to match the
beginning of the line).

/\/$/ -this regexp means:
match a / at the end of the line.

I think you need to read some docs about regular expressions..
Start with this one:
http://www.perl.com/doc/manual/html/pod/perlre.html
(javascript does not support all perl regexps modifiers but i think it
is a good point to start)

About your question.. you can use a single regexp (Bart solution is
actually working but regexps are so powerful)

Try this one:

var stringa='/AAA/B/file.jpg';
//same note about Evertjan about using string as a var name..
var var_2=stringa.replace(/\/([^\/]+$)/,'zzz$1');
alert(var_2);
alert(RegExp.$1); //the file name

My regexp can be translated to:

match a /
then match any character that is not a / and keep going since you
reach the end of the line.
Then, replace / with zzz and all chars we saved on the first group.

Ciao.
Seba
Feb 25 '07 #5
den
very thanks at all;

only a small question

in the last example
you use
alert(RegExp.$1);

but this parameter RegExp function in all modern browser?
in all version of javascript?
Feb 25 '07 #6
den
Il 25 Feb 2007 02:24:38 -0800, g4toloc0 ha scritto:
:)

Evertjan has posted my same solution.. but i have added a little
explanation so there's my answer to the problem.
>if I use this :
/\/$/
why not work ?
I have insert the $ at the end

As Bart is correctly saying '$' is not a 'last occurrence identifier'.
It is used to match the end of the line (while ^ is used to match the
beginning of the line).

/\/$/ -this regexp means:
match a / at the end of the line.

I think you need to read some docs about regular expressions..
Start with this one:
what is the difference between this (Evertjian)
/\/([^\/]*)$/
and your:
/\/([^\/]+$)/

your $ is inner () and have a + .

CAn you does examples of difference?


only a small question

in the last example
you use
alert(RegExp.$1);

but this parameter RegExp function in all modern browser?
in all version of javascript?

very thanks at all;
Feb 25 '07 #7
what is the difference between this (Evertjian)
/\/([^\/]*)$/
and your:
/\/([^\/]+$)/
* Match 0 or more times
+ Match 1 or more times

that means that Evertjian code will match something like:
var stringa='/AAA/B/';
While my code will not.

My regexp says:
When you found a '/', search at least one character that is not '/'
before reaching the end of the line.

In other words Evertjian RE does not need a file name after the last
'/'
your $ is inner () and have a + .

CAn you does examples of difference?
Caret and dollar are a bit 'weird'... they match a position in the
line rather than a char.
So (at least for this example) ')$' is the same as '$)'... but
Evertjian ')$' is a little more 'syntactically correct'.
[you often need to deal with '$)' when building very complex
regepxs]
in the last example
you use
alert(RegExp.$1);
but this parameter RegExp function in all modern browser?
in all version of javascript?
you need at least javascript 1.2 (a.k.a JScript, ECMAScript or
ECMA-262)
It is supported by IE4+, NS4+, FF (all versions) and most other modern
browsers.

Mmmm... enough talking for now..

Ciao, ciao.
Seba.

Feb 25 '07 #8
In comp.lang.javascript message <yg****************************@40tude.n
et>, Sun, 25 Feb 2007 11:43:45, den <sp*****@not.notposted:
>very thanks at all;

only a small question

in the last example
you use
alert(RegExp.$1);

but this parameter RegExp function in all modern browser?
in all version of javascript?
Possibly; but the usage is not given in ISO/IEC 16262. It is deprecated
for javascript, and one should instead index the result of .exec or
..match. See in <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.

I have heard that in JScript.NET with ASP.NET the RegExp.$1 notation is
not available.

Query : I want to find a way of detecting all and only instances of such
deprecated usage on my site, for removal. Seeking RegExp.$ is of course
insufficient.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 25 '07 #9
On Feb 25, 4:06 pm, Dr J R Stockton <reply0...@merlyn.demon.co.uk>
wrote:
Possibly; but the usage is not given in ISO/IEC 16262. It is deprecated
for javascript, and one should instead index the result of .exec or
.match. See in <URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
uh, it's true. even if using the regexp object like this is so
uncool...
http://developer.mozilla.org/en/docs...cated_Features
Query : I want to find a way of detecting all and only instances of such
deprecated usage on my site, for removal. Seeking RegExp.$ is of course
insufficient.

It's a good idea to read the newsgroup and its FAQ. See below.
mmm. is this a request or are you just quoting a FA Question???

Ciao,
Seba

Mar 2 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2133
by: EFP | last post by:
Can anyone help me with a simple regular expression problem. All that I want to do is take a list of known data and extract a particular section of the string to form a new list. Here is my...
11
5350
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
6
489
by: JohnSouth | last post by:
Hi I've been using a Regular expression to test for valid email addresses. It looks like: \w+(\w+)*@\w+(\w+)*\.\w+(\w+)* I've now had 2 occassions where it has rejected and email address...
18
3015
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
5
3089
by: Ryan | last post by:
HELLO I am using the following MICROSOFT SUGGESTED (somewhere on msdn) regular expression to validate email addresses however I understand that the RFP allows for "+" symbols in the email address...
6
2276
by: Ludwig | last post by:
Hi, i'm using the regular expression \b\w to find the beginning of a word, in my C# application. If the word is 'public', for example, it works. However, if the word is '<public', it does not...
25
5130
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
18
622
by: Lit | last post by:
Hi, I am looking for a Regular expression for a password for my RegExp ValidationControl Requirements are, At least 8 characters long. At least one digit At least one upper case character
1
3380
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
14
4959
by: Andy B | last post by:
I need to create a regular expression that will match a 5 digit number, a space and then anything up to but not including the next closing html tag. Here is an example: <startTag>55555 any...
0
7259
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7158
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7380
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.