473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

RegEx noob question

I need to take a string and allow only the first occurrence of an
apostrophe for the validation of a city name...

Example: D'Iberville, MS

Say someone enters D'I'berville, MS by mistake..

I'm able to retain all the apostrophes with the following:

city.replace(/[^a-zA-Z0-9\'\$\,\.\#\s]/g,"");

I want to retain the first apostrophe only. I've tried adding {2,} in
with the apostrophe, but no luck.

An example of the proper way to do this would be greatly appreciated.

Thanks.
Sep 19 '08 #1
3 1192
On Sep 19, 6:01*am, "neptune6ju...@gmail.com"
<neptune6ju...@gmail.comwrote:
* * city.replace(/[^a-zA-Z0-9\'\$\,\.\#\s]/g,"");
In string replace method, second parameter can be a function. You can
make use of this like:
-----------
var str = "D'I'ber'v'i'lle";
var myRe = /([^']*')(.*)/;

str = str.replace(myRe,
function (matchStr, strInParanthesis1, strInParanthesis2) {
return strInParanthesis1 + strInParanthesis2.replace(/'/g,
"");
}
);
-----------

Output:
"D'I'ber'v'i'lle" will become "D'Iberville"
"'DIber'v'i'lle" will become "'DIberville"

- Kiran Makam

Sep 19 '08 #2
On Sep 19, 2:01*am, "neptune6ju...@gmail.com"
<neptune6ju...@gmail.comwrote:
I want to retain the first apostrophe only.
S = "1'2'3'4'5"
S.replace("'", "\xFF").replace(/'/g, "").replace("\xFF", "'")

There, \xFF can be replaced by any chatacter that cannot occur in the
input.

S = "1'2'3'4'5"
P = S.indexOf("'")
S = S.substring(0,P+1) + S.substring(P+1).replace(/'/g, "")

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ...
Sep 20 '08 #3
Dr J R Stockton wrote:
<neptune6ju...@gmail.comwrote:
>I want to retain the first apostrophe only.

S = "1'2'3'4'5"
S.replace("'", "\xFF").replace(/'/g, "").replace("\xFF", "'")
I think this would be the most efficient, yes. If there were a single
regex, it would go like this:

/(?<=')([^']*)'/g,'$1'

or

/(?<=')(.*?)'/g,'$1'

But javascript only supports look-ahead (?=/?!) and not look-behind (?
<=/?<!); so there is no other choice than finding a workaround. It
already struck me a few times that javascript has some gaps in the
language: no regex look-behind, no reverse()-property for strings, ...

--
Bart
Sep 21 '08 #4

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

Similar topics

4
by: engwar1 | last post by:
Not sure where to ask this. Please suggest another newsgroup if this isn't the best place for this question. I'm new to both vb.net and regex. I need a regular expression that will validate what...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
6
by: Du Dang | last post by:
Text: ===================== <script1> ***stuff A </script1> ***more stuff <script2> ***stuff B
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: Chris | last post by:
How Do I use the following auto-generated code from The Regulator? '------------------------------------------------------------------------------ ' <autogenerated> ' This code was generated...
6
by: Martin Evans | last post by:
Sorry, yet another REGEX question. I've been struggling with trying to get a regular expression to do the following example in Python: Search and replace all instances of "sleeping" with "dead"....
7
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
9
by: neptune6jun44 | last post by:
RegEx noob here, so apologies for such an easy issue. :) I have a string like A:BCD:EFG and I need to populate a variable with 'BCD'. What is the RegEx syntax for matching the content between...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...

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.