/    Sign up×
Community /Pin to ProfileBookmark

Can I get a reg expression to sum all this up?

Ok I’m designing a facoring function that will take a string-number
containing numbers from 0-7 (octal radix=8) and
Every time that the function detects exactly 3 numbers of any one number, then it’ll remove all three.
So if the function accpeted an argument of, say—”034202210223″
the function will first sort the numbers to “000122222334”
then remove all instances of 3-in-a-row and thus returning removeing
the numbers in red “[color=red]000[/color]1[color=red]222[/color]22334″ and the function will finally return “122334” after receiving
an argument of “034202210223”.

[size=+1]Here’s my function as of now:[/size]

[code]
function factormove(){
var string_number=arguments[0].split(“”).sort().join(“”)
string_number=string_number.replace(/000/g,”a”).replace(/111/g,”a”)
.replace(/222/g,”a”).replace(/333/g,”a”).replace(/444/g,”a”)
.replace(/555/g,”a”).replace(/666/g,”a”).replace(/777/g,”a”)
.replace(/a/g,””);
return string_number
}
//all the replaces are supposed to be on one line
[/code]

So it there a way to put all them replaces into one statement?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@baconbuttyMar 10.2005 — Try:-


string_number=string_number.replace(/(d)11/g,"")


the "1" references the digit captured by "(d)"


[URL]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsgrpregexpsyntax.asp[URL]
Copy linkTweet thisAlerts:
@UltimaterauthorMar 10.2005 — OMG! thanks! I didn't know how to replace $1 and $2... from perl

into javascript! 1 and 2 who would have thought!?
Copy linkTweet thisAlerts:
@UltimaterauthorMar 10.2005 — Man it worked like a charm I tell you!

I also went a step further, since the numbers going into this

function will be in octal, I didn't want to touch the number 8 and 9.

string_number=string_number.replace(/([color=red][0-7][/color])11/g,"")


Threee cheers for baconbutty!

Hip-Hip-Hoary!{3}
Copy linkTweet thisAlerts:
@baconbuttyMar 11.2005 — cool:

Glad to be of help.
×

Success!

Help @Ultimater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...