Greg Wozniak
Greg Wozniak

Reputation: 7172

Regex - replace all spaces except a space after a single letter

In JavaScript I would like to replace all spaces that don't match the following Regex with percentages (%):

\b[a-zA-Z]\s\b

I try to exclude all the spaces that sit next to a single character and replace all the other. In case:

True that A B C School is great for kids and C D E School is not

all the spaces except these between A-B and B-C and C-S should be replaced with percentages. So the result is:

True%that%A B C School%is%great%for%kids%and%C D E School%is%not

I read:

  1. Regex negative match query
  2. Replace part of string that doesn't match regex

but it didn't give me the ultimate answer.

Currentlyquery.replace(\b[a-zA-Z]\s\b,'%') replaces exactly the spaces that should be left as they are.

Could you advise how to negate my expression in the correct way?

Upvotes: 3

Views: 3745

Answers (5)

MT0
MT0

Reputation: 167774

You can match either:

  • a non-word boundary \B followed by a non-whitespace character \S; or
  • the start of the string ^

Followed by one-or-more whitespace characters \s+ using the regular expression:

/(\B\S|^)\s+/g

For example:

var regex = /(\B\S|^)\s+/g;

var tests = [
  " ",
  " A B C Test DE",
  "True that A B C School is great for kids and C D E School is not"
];

for ( var i = 0; i < tests.length; i++ )
{
  console.log( `"${tests[i]}" -> "${tests[i].replace( regex, '$1%' )}"` );
}

Upvotes: 0

Semi-Friends
Semi-Friends

Reputation: 480

Check this out. hope it helps. what it does is it first find the match needed at least 2 letters before a space, then it replaces it with a pipe'|'. just change it to whatever you want. you can use this as your base and expand from here if you have more conditions to add

var myString = 'A B C School is great for kids';
    
var result = myString.replace(/\w{2}\s/gi, match => match.replace(' ', '|'))

document.writeln(result)

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

Match and capture the pattern you have, and just match any other whitespace in other contexts:

var s = "True that A B C School is great for kids and C D E School is not";
var rx = /\b([a-zA-Z]\s)\b|\s/g;
console.log(
  s.replace(rx, function($0,$1) { return $1 ? $1 : "%"; })
  // ES6+ syntax
  // s.replace(rx, ($0,$1) => $1 ? $1 : "%" )
);
// => True%that%A B C School%is%great%for%kids%and%C D E School%is%not

Details

  • \b([a-zA-Z]\s)\b - a word boundary, then any ASCII letter and a whitespace (captured into Group 1) followed with a word boundary
  • | - or
  • \s - 1 whitespace char

Inside the anonymous method (or arrow method), $0 stands for the whole match and $1 stands for the captured text (Group 1 value). If Group 1 matched, we just return its value (so, the whitespaces after 1 letter words are kept). Else, the whitespace matched is replaced with %.

Upvotes: 2

KooiInc
KooiInc

Reputation: 122888

Try

console.log("[foo bar foobar   barfoo b f f b ]".replace(/([a-z]{2,})(\s+)/gi, "$1#"));

Upvotes: 0

Leethium
Leethium

Reputation: 1

There is no need to negate any expression, you could just match "Two or more characters followed by a space"

Replace

([a-zA-Z]{2,})\s

By

$1

Upvotes: 0

Related Questions