...

View Full Version : RegEx - replace all spaces EXCEPT those followed by a hyphen



WolfShade
06-27-2012, 09:16 PM
Hello, everyone.

I'm trying to write a Regular Expression replace script that will replace all spaces within a string with a space+, UNLESS the space is followed by a hyphen "-".

I'm still a n00b with RegEx, despite the fact that I've been using RegEx for years. Just never had to do something like this, before.

I've tried using /[ [^-]]/g and that doesn't work, as well as /[ {0,}-*/g. Any suggestions?

Thanks,

Philip M
06-27-2012, 09:27 PM
Have I understood you correctly? replace all spaces by a + unless the space is followed by a hyphen -.


<script type = "text/javascript">

var t = "Peter - Paul and Percy are - senior coders";
var nt = t.replace(/\s(?!-)/gi,"+"); // the ? is essential!

alert (nt);

</script>


He has won six million dollars in as many years. - Presenter, Channel 5

Old Pedant
06-27-2012, 09:29 PM
var re = / ([^\-])/g;
var s = "try this demo -does it work?";
var changed = s.replace( re, " +$1" );

It works for all except a space at the very end of the string. We could handle that, too, if you cared.

Old Pedant
06-27-2012, 09:31 PM
Ahh... Philip's is more elegant, but he changed space to just + instead of space+, which I think is what you wanted. Trivial to change that if my interpretation is correct.

Philip M
06-27-2012, 09:40 PM
Ahh... Philip's is more elegant, but he changed space to just + instead of space+, which I think is what you wanted. Trivial to change that if my interpretation is correct.


var nt = t.replace(/\s(?!-)/gi," +"); // the ? is essential!

I was not sure of exactly what the OP required.

WolfShade
06-27-2012, 09:41 PM
Thank you, both! I will give them both a shot and report back.

WolfShade
06-27-2012, 09:47 PM
Brilliant!! Thank you, Philip M, it works exactly as I need (after adding a space before the plus).

Yeah, I know.. odd.. why a space before the plus? To search a ColdFusion Solr collection (the + means all words will be required, not just OR, and I'm replacing all instances of "NOT" with a " -" before this point.)

When I enter "air force NOT movement", the returned string is (again, exactly as I need it): " +air +force -movement". (Search for documents that have both air and force, but do not have the word movement.)

Thanks, again!

Philip M
06-28-2012, 08:00 AM
Brilliant!! Thank you, Philip M, it works exactly as I need (after adding a space before the plus).

Yeah, I know.. odd.. why a space before the plus? To search a ColdFusion Solr collection (the + means all words will be required, not just OR, and I'm replacing all instances of "NOT" with a " -" before this point.)

When I enter "air force NOT movement", the returned string is (again, exactly as I need it): " +air +force -movement". (Search for documents that have both air and force, but do not have the word movement.)

Thanks, again!

Ah, I was wondering what the practical usage was! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum