Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Advanced Replace

Status
Not open for further replies.

Smoogan

Programmer
Feb 21, 2008
21
0
0
GB
Hello everyone,

I have the following replace function:

item.value = item.value.replace(/[.](\s{1,2})?/g, ".").replace(/[.]/g, ". ");

Ok. My problem is and I'm not really sure where to start is that I want to only replace the full stops that have a Alphabetic character preceding it.

Any ideas... I just want a push in the right direction.

Thanks
Smoogan
 
>item.value = item.value.replace(/[.](\s{1,2})?/g, ".").replace(/[.]/g, ". ");
I am going to keep this part of yours which is a bit ambiguous in intention.

>...that I want to only replace the full stops that have a Alphabetic character preceding it.
It would then be this, for instance.

[tt]item.value = item.value.replace(/([a-zA-Z][.])(\s{1,2})?/g, "$1").replace(/([a-zA-Z][.])/g, "$1 ");[/tt]
 
Hi tsuji,

Thanks for that, can you esplain alittle more on what excatly you done?

Also how would you change my orginal code? How you give an example.

thanks
 
Any where (in both replace) {[a-zA-Z][.]) : a period preceded with an alphabet is retained (ie, "$1", the first group of submatches). In the 2nd replace, the part (\s{1,2})? is discarded. In the 1st replace, an empty space is added to that particle.

If the period is not preceded with an alphabetic, nothing will be matched and hence nothing being replaced.
 
I'm still a little confused. At least I know it works.
 
If the original pattern is made by you, you should be in a position to understand what I explained. If it is not, I cannot give a whole lecture on that, it would be very pretentious for those in the know.

I go googling and choose this among others for you to read.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top