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

javascript - Regex replace all character except last 5 character and whitespace with plus sign - Stack Overflow

programmeradmin1浏览0评论

I wanted to replace all characters except its last 5 character and the whitespace with +

var str = "HFGR56 GGKDJ JGGHG JGJGIR"
var returnstr = str.replace(/\d+(?=\d{4})/, '+');

the result should be "++++++ ++++ +++++ JGJGIR" but in the above code I don't know how to exclude whitespace

I wanted to replace all characters except its last 5 character and the whitespace with +

var str = "HFGR56 GGKDJ JGGHG JGJGIR"
var returnstr = str.replace(/\d+(?=\d{4})/, '+');

the result should be "++++++ ++++ +++++ JGJGIR" but in the above code I don't know how to exclude whitespace

Share Improve this question edited Nov 3, 2019 at 14:00 Newboy11 asked Nov 3, 2019 at 13:51 Newboy11Newboy11 3,1448 gold badges31 silver badges48 bronze badges 3
  • Are you sure you're asking about C#? This looks more like JavaScript to me. – Tim Pietzcker Commented Nov 3, 2019 at 13:59
  • @TimPietzcker sorry about that. yes its javascript – Newboy11 Commented Nov 3, 2019 at 14:00
  • 1 If there's always at least 2 words, another idea to search for \w(\W+\w+$)? and replace with +$1 – bobble bubble Commented Nov 3, 2019 at 15:24
Add a ment  | 

4 Answers 4

Reset to default 3

You need to match each character individually, and you need to allow a match only if more than six characters of that type follow.

I'm assuming that you want to replace alphanumeric characters. Those can be matched by \w. All other characters will be matched by \W.

This gives us:

returnstr = str.replace(/\w(?=(?:\W*\w){6})/g, "+");

Test it live on regex101..

The pattern \d+(?=\d{4}) does not match in the example string as is matches 1+ digits asserting what is on the right are 4 digits.

Another option is to match the space and 5+ word characters till the end of the string or match a single word character in group 1 using an alternation.

In the callback of replace, return a + if you have matched group 1, else return the match.

\w{5,}$|(\w)

Regex demo

let pattern = / \w{5,}$|(\w)/g;
let str = "HFGR56 GGKDJ JGGHG JGJGIR"
  .replace(pattern, (m, g1) => g1 ? '+' : m);
console.log(str);

Another way is to replace a group at a time where the number of +
replaced is based on the length of the characters matched:

var target = "HFGR56 GGKDJ JGGHG JGJGIR";

var target = target.replace(
        /(\S+)(?!$|\S)/g, 
        function( m, g1 )
        {
          var len = parseInt( g1.length ) + 1;
          //return "+".repeat( len );     // Non-IE (quick)
          return Array( len ).join("+");  // IE (slow)
        } );
        
console.log ( target );        

You can use negative lookahead with string end anchor.

\w(?!\w{0,5}$)

Match any word character which is not followed by 0 to 5 characters and end of string.

var str = "HFGR56 GGKDJ JGGHG JGJGIR"
var returnstr = str.replace(/\w(?!\w{0,5}$)/g, '+');

console.log(returnstr)

发布评论

评论列表(0)

  1. 暂无评论