/    Sign up×
Community /Pin to ProfileBookmark

Strange kind of replace

I have this:

[CODE]replace(/(S)([A-Z])/g,’$1 $2′);[/CODE]

It puts a space before all capital letters.

What I am looking for it to do is to only put space before capital letters that have a non capital letter behind it. Ex. [COLOR=”SeaGreen”]ABHelloWorld[/COLOR] would become [COLOR=”SeaGreen”]AB Hello World[/COLOR]

I tried this:

[CODE]replace(/(S)([A-Z][a-z])/g,’$1 $2′);[/CODE]

But it only puts a space before one capital letter.

Any ideas?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rpgfan3233Jun 26.2008 — replace(/(S)([A-Z][^A-Zs])/g, '$1 $2');
Does that do what you need?

That simply attempts to find a capital letter [A-Z] followed by a character that is not [^] a capital letter [A-Z] or a space [s] and separate them with a space if necessary.
Copy linkTweet thisAlerts:
@Declan1991Jun 26.2008 — var str = "ABHelloWorld";
alert(str.replace(/([A-Z])(?=[a-z])/g," $1"));

A capital letter followed by a lower-case letter.

var str = "ABHelloWorld";
alert(str.replace(/([A-Z])(?=[^A-Z])/g," $1"));

A capital letter followed by a non-capital letter.
Copy linkTweet thisAlerts:
@spadi33authorJun 26.2008 — replace(/(S)([A-Z][^A-Zs])/g, '$1 $2');
Does that do what you need?

That simply attempts to find a capital letter [A-Z] followed by a character that is not [^] a capital letter [A-Z] or a space [s] and separate them with a space if necessary.[/QUOTE]


That did exactly what I asked. But I tried it on another level.

I rearanged the letters: [COLOR="Red"]HelloBigWorldAB[/COLOR]

[CODE]var str='HelloBigWorldAB';
var outcome=str.replace(/(S)([A-Z][^A-Zs])/g, '$1 $2');
document.write(outcome);
[/CODE]


It writes:

[COLOR="Red"]Hello Big WorldAB[/COLOR]

Is it possible to have that write [COLOR="SeaGreen"]Hello Big World AB[/COLOR]

as well as [COLOR="SeaGreen"]AB Hello Big World[/COLOR] ?
Copy linkTweet thisAlerts:
@Declan1991Jun 26.2008 — Mine does it.var str = "HelloBigWorldAB";
alert(str.replace(/([A-Z])(?=[^A-Z])/g," $1"));
Copy linkTweet thisAlerts:
@Logic_AliJun 26.2008 — I think this does what you intend:
alert( "|HelloBigWORLdHere".replace(/([^A-Z])([A-Z])/g,"$1 $2") )
Copy linkTweet thisAlerts:
@spadi33authorJun 26.2008 — I combined the two...

[CODE]var rts=str.replace(/([^A-Z])([A-Z])/g,"$1 $2");
var outcome=rts.replace(/([A-Z])(?=[^A-Z])/g," $1");
document.write(outcome);
[/CODE]


Works like a charm. Thanks for the help!
×

Success!

Help @spadi33 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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