Replace underscore between words (reg.exp)

Posted by lasseespeholt on Stack Overflow See other posts from Stack Overflow or by lasseespeholt
Published on 2010-03-08T17:53:41Z Indexed on 2010/03/08 18:21 UTC
Read the original article Hit count: 292

Filed under:
|
|
|
|

Hey,

I need a regular expression to solve the following problem (links to similar problems is also appreciated, related tutorials etc.):

__some_words_a_b___ => __some words a b___
____ => ____

So I want underscores between words to be replaced with space and keep leading and trailing underscores. I found this:

^[ \t]+|[ \t]+$

and I guess it most be something like that. I will use it in jQuery, Java (stdlibs) and maybe XSLT.

Addition: The sentences do not necessarily start with underscores or ends with underscores. It is also possible that a sentence ain't containing underscores at all.

Best regards Lasse Espeholt

© Stack Overflow or respective owner

Related posts about regex

Related posts about replace