最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - regex - add space before any character that is not a letter - Stack Overflow

programmeradmin3浏览0评论

I have tried many things and am looking for a space to be added after any character that is not a letter. Currently I can replace all non letter characters with a space, but I want a space before the character is matched. At the moment I have:

var str = 'div#some_id.some_class';
str = str.replace(/[^A-Za-z0-9]/g, ' ');

This provides me with the following,

div some_id some_class

however I am looking for the result to be div #some_id .some_class

Any help would be greatly appreciated.

I have tried many things and am looking for a space to be added after any character that is not a letter. Currently I can replace all non letter characters with a space, but I want a space before the character is matched. At the moment I have:

var str = 'div#some_id.some_class';
str = str.replace(/[^A-Za-z0-9]/g, ' ');

This provides me with the following,

div some_id some_class

however I am looking for the result to be div #some_id .some_class

Any help would be greatly appreciated.

Share Improve this question edited Jun 3, 2015 at 10:21 karthik manchala 13.7k1 gold badge33 silver badges55 bronze badges asked Jun 3, 2015 at 10:11 Paul FitzgeraldPaul Fitzgerald 12.1k4 gold badges45 silver badges57 bronze badges 2
  • is föóbàr to f ö ób àr (and things along this line) also the result of what you want? Or would you have a smaller subset of characters in mind which you specifically consider to be matches (like ., # etc)? – GitaarLAB Commented Jun 3, 2015 at 10:30
  • 1 the answer below provides what I am looking for, but I am only looking for a smaller subset ie. ., # – Paul Fitzgerald Commented Jun 3, 2015 at 10:31
Add a ment  | 

5 Answers 5

Reset to default 4

You can use a negative lookahead for this:

str = str.replace(/(?!\w|$)/g, ' ')
//=> "div #some_id .some_class"

(?!\w|$) will match positions where next character is not alpha0numeral or end of line.

You can try this :

str = str.replace(/([^a-zA-Z0-9_])/g, ' $1') //Omit 0-9 if you you want a space before them too

//OUTPUT: div #some_id .some_class

Use groups and substitution

var str = 'div#some_id.some_class';
str = str.replace(/([^A-Za-z0-9])/g, ' $1');
console.log(str);

If you have an exact subset in mind, you'd probably be better of to specify just (but exactly) that (since Unicode has quite some characters you might not have even thought about.. think about things like unintended föóbàr to f ö ób àr, not even mentioning languages that don't really use A-z):

var str = 'div#some_id.some_class';
str = str.replace( /[.#]/g    // add your exact subset to match on between the [ ]
                 , ' $&'      // $& inserts the matched substring (no capturing group needed)
                 );
console.log(str);

Also try at \b word boundaries, where a \W non word character is ahead:

(?=\W)\b and replace matched position with a space (regex101).

发布评论

评论列表(0)

  1. 暂无评论