473,320 Members | 1,977 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,320 software developers and data experts.

RegEx re Replace

Can someone help me out?

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

Specifically, this is to display credit card numbers.
alert(doit("4444555566667777"));

// should return "4444 5555 6666 7777"

function doit(ccno) {
if (ccno.length % 4 != 0) return;
var what = ??????
return what;
}

Here's a non-regular expression approach:

var what = "";
for (var i=1; i<ccno.length/4+1; i++) {
if (what != "") what += " ";
what += ccno.substring(i*4-4,i*4);
}

Thanks in advance.
Jul 23 '05 #1
7 1932
Ivo
"McKirahan" wrote

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

Specifically, this is to display credit card numbers.


'1111222233334444'.replace( /(\w{4})/g, '$1 ' ).replace(/ +$/,'');

The first replacement puts the spaces in, the second one removes the
trailing space(s).
hth
--
Ivo
Jul 23 '05 #2
McKirahan wrote:
Can someone help me out?

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

Specifically, this is to display credit card numbers.
alert(doit("4444555566667777"));

// should return "4444 5555 6666 7777"

function doit(ccno) {
if (ccno.length % 4 != 0) return;
var what = ??????
return what;
}

Here's a non-regular expression approach:

var what = "";
for (var i=1; i<ccno.length/4+1; i++) {
if (what != "") what += " ";
what += ccno.substring(i*4-4,i*4);
}

Thanks in advance.


x = x.replace(/(\d{3}\d\B)/g, '$1 ');

Jul 23 '05 #3
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
McKirahan wrote:
Can someone help me out?

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

[snip]
x = x.replace(/(\d{3}\d\B)/g, '$1 ');


Excellent!

Any recommendations on learning RegEx?

I have O'Reilly's "Mastering Regular Expressions"
but I haven't spent the time with it yet.
Jul 23 '05 #4
McKirahan wrote:
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
McKirahan wrote:
Can someone help me out?

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

[snip]
x = x.replace(/(\d{3}\d\B)/g, '$1 ');


Excellent!

Any recommendations on learning RegEx?

I have O'Reilly's "Mastering Regular Expressions"
but I haven't spent the time with it yet.


That O'Reilly book is phenomenal (although notably missing *any*
JavaScript implementations). I blow at regexes - but, for what it's
worth, try these...

http://www.webreference.com/js/column5/
http://www.regular-expressions.com/
http://www.devarticles.com/c/a/JavaS...in-JavaScript/
http://d0om.fnal.gov/d0admin/doctaur...pt/ch10_01.htm
http://www.evolt.org/article/Regular...ript/17/36435/
Any recommendations on learning RegEx?


Get a big bottle of Advil first. #:=o

Jul 23 '05 #5
McKirahan wrote:
"RobB" <fe******@hotmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... ....
I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).
....x = x.replace(/(\d{3}\d\B)/g, '$1 ');

.... Any recommendations on learning RegEx?

....

I learned from
http://php.net/manual/en/reference.p...ern.syntax.php
Dense, with many goodies javascript doesn't have,
but with many good examples, too, especially in the second half.

Csaba Gabor from Vienna
Jul 23 '05 #6
JRS: In article <Ns********************@comcast.com>, dated Thu, 31 Mar
2005 11:26:13, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
Can someone help me out?

I'm looking for a regular expression that inserts a space
into a string after every fourth byte (except the last).

Specifically, this is to display credit card numbers.


Look for code that inserts thousands separators, change comma to space
and 3 to 4 - js-maths.htm .

That's if
T = S.replace(/^(\d{4})(\d{4})(\d{4})(\d{4})$/, "$1 $2 $3 $4")
does not please you.

CC numbers all have 16 digits, do they not?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #7
"Dr John Stockton" <sp**@merlyn.demon.co.uk> wrote in message
news:pf**************@merlyn.demon.co.uk...

[snip]
CC numbers all have 16 digits, do they not?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE

4 ©
I think American Express and Diners Club are 15 digits.

http://money.howstuffworks.com/credit-card2.htm
http://javascript.internet.com/forms...edit-card.html
Jul 23 '05 #8

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

Similar topics

3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
7
by: bill tie | last post by:
I'd appreciate it if you could advise. 1. How do I replace "\" (backslash) with anything? 2. Suppose I want to replace (a) every occurrence of characters "a", "b", "c", "d" with "x", (b)...
6
by: tshad | last post by:
Is there a way to use Regex inside of a tag, such as asp:label? I tried something like this but can't make it work: <asp:label id="Phone" text=Regex.Replace('<%# Container.DataItem("Phone")...
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 ...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
3
by: Craig Buchanan | last post by:
Is there a way to combine these two Replace into a single line? Regex.Replace(Subject, "\&", "&amp;") Regex.Replace(Subject, "\'", "&apos;") Perhaps Regex.Replace(Subject, "{\&|\'}", "{&amp;|&apos;}")...
9
by: Whitless | last post by:
Okay I am ready to pull what little hair I have left out. I pass the function below my String to search, my find string (a regular expression) and my replace string (another regular expression)....
4
by: Morgan Cheng | last post by:
In my case, I have to remove any line containing "0.000000" from input string. In below case, it takes about 100 ms for 2k size input string. Regex.Replace(inputString, ".*0\\.000000.*\n", ""); I...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
0
by: Karch | last post by:
I have these two methods that are chewing up a ton of CPU time in my application. Does anyone have any suggestions on how to optimize them or rewrite them without Regex? The most time-consuming...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.