1

Given an input text such where all spaces are replaced by n _ :

Hello_world_?. Hello_other_sentenc3___. World___________. 

I want to keep the _ between words, but I want to stick each punctuation back to the last word of a sentence without any space between last word and punctuation. I want to use the the punctuation as pivot of my regex.

I wrote the following JS-Regex:

str = str.replace(/(_| )*([:punct:])*( |_)/g, "$2$3"); 

This fails, since it returns :

Hello_world_?. Hello_other_sentenc3_. World_._ 

Why it doesn't works ? How to delete all "_" between the last word and the punctuation ? http://jsfiddle.net/9c4z5/

2
  • I don't believe JS has a :punct: option in its regex implementaiton. Commented Nov 9, 2013 at 21:27
  • Have you an idea of what should I use instead then ? Commented Nov 9, 2013 at 21:28

2 Answers 2

4

Try the following regex, which makes use of a positive lookahead:

str = str.replace(/_+(?=\.)/g, ""); 

It replaces all underscores which are immediately followed by a punctuation character with the empty string, thus removing them.

If you want to match other punctuation characters than just the period, replace the \. part with an appropriate character class.

Sign up to request clarification or add additional context in comments.

5 Comments

I need all the punctuations, my end-users are multilingual, use multiple scripts.
@Hugolpz Have you tried to implemented what I explain in the last paragraph?
I'am begginer, I'am faking that I understand what is "character class" but want the precise answer, while I actually just don't understand your answer ("character class"). Just that.
@Hugolpz Just list the punctuation characters that you want to match: /_+(?=[.?!;,])/g
Marius: thanks. So a "character class" is the list of characters you place within [] I guess
1

JavaScript doesn't have :punct: in its regex implementation. I believe you'd have to list out the punctuation characters you care about, perhaps something like this:

str = str.replace(/(_| )+([.,?])/g, "$2"); 

That is, replace any group of _ or space that is immediately followed by punctation with just the punctuation.

Demo: http://jsfiddle.net/9c4z5/2/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.