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

jquery - JavaScript replace first 5 characters with * - Stack Overflow

programmeradmin4浏览0评论

I need Javascript Regex to replace the first 5 characters. Below are a few examples. The first line is the input and the second is the expected output. Could you please let me know how to achieve this?

I have tried follwoing. But none of them is working if the input is less than 4.

.{5} to ***** and ^\d{5} to *****

Examples

123456789
XXXXX6789

123
XXX

123456
XXXXX6

1
X

12345
XXXXX

I need Javascript Regex to replace the first 5 characters. Below are a few examples. The first line is the input and the second is the expected output. Could you please let me know how to achieve this?

I have tried follwoing. But none of them is working if the input is less than 4.

.{5} to ***** and ^\d{5} to *****

Examples

123456789
XXXXX6789

123
XXX

123456
XXXXX6

1
X

12345
XXXXX
Share Improve this question asked Aug 30, 2019 at 18:14 DebopamDebopam 3,3567 gold badges43 silver badges76 bronze badges 1
  • 1 For the latest Chrome and Node versions, .replace(/(?<=^\d{0,4})\d/g, '*') – Wiktor Stribiżew Commented Aug 30, 2019 at 18:53
Add a comment  | 

7 Answers 7

Reset to default 6

You may use a callback function or lambda in .replace():

var arr = ['123456789',
'123',
'123456',
'1',
'12345'];

arr.forEach(el => console.log(el, '::', el.replace(/^\d{1,5}/, 
            m => m.replace(/\d/g, 'X'))))

Your first regex needs a little tweak, this should work.

let reg = /.{1,5}/
let string = '123456789';
let string2 = '123';
console.log(string.replace(reg, (m) => "X".repeat(m.length)));
console.log(string2.replace(reg, (m) => "X".repeat(m.length))); 

If you are not looking for regex solution ,then you can try this as an option as well.An alternative based on substring() method

function replace_String(string, numberofchar,chartoreplace) {

  return string.substring(0, numberofchar).split("").map(ele => ele = chartoreplace).join("").concat(string.substring(numberofchar, string.length))

}


console.log(replace_String("123456789", 5,"X"))
console.log(replace_String("1", 1,"*"))

Many ways you can do it. One is two replace statements

const hideFive = str => str.replace(/^\d{1,5}/, x => x.replace(/./g, '*'))

var tests = ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "1234567890"]
tests.forEach( val => console.log(val, '=', hideFive(val)) )

without fat arrows

function hideFive (str) {
  return str.replace(/^\d{1,5}/, function(x) {
    return x.replace(/./g, '*')
  })
}

var tests = ["1", "12", "123", "1234", "12345", "123456", "1234567", "12345678", "1234567890"]
tests.forEach( function(val) {
  console.log(val, '=', hideFive(val))
})

You could replace the first characters and take the sliced rest of the string.

const replaceFirst5 = s => '*'.repeat(Math.min(5, s.length)) + s.slice(5);

console.log(replaceFirst5("123456789"));
console.log(replaceFirst5("123"));

You can use replace and repeat

 ^.{1,5}
  • ^ - Start of string
  • .{1,5} - Match anything except new line, least one and max 5 time

let replaceFirst5 = (str) =>{
  return str.replace(/^.{1,5}/, m=> "X".repeat(m.length))
}

console.log(replaceFirst5("123456789"))
console.log(replaceFirst5("123"))

supposing that these numbers are wrapped in a class named "numbers"

jQuery code

$.each('.numbers',function(){
let num = $(this).text();
var j="";
  for(let i =0;i<num.length;i++){
  if( i == 5){
  break;
  }
 j+="*";
  }
  var res = str.replace(num.substring(0, 5),j);
  document.getElementById("demo").innerHTML = res;
    });
发布评论

评论列表(0)

  1. 暂无评论