Home  >  Article  >  Web Front-end  >  给文字加上着重号的JS代码_javascript技巧

给文字加上着重号的JS代码_javascript技巧

WBOY
WBOYOriginal
2016-05-16 17:15:571217browse
复制代码 代码如下:

var s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890';
function f(s, width){
  return s.replace(
  new RegExp('[\\s\\S]{1,'+(width || 4)+'}', 'g'),
  function(m){
    return '^' + new Array(m.length).join(' ');
  });
}

console.log(s);
console.log(f(s, 4));
console.log(f(s, 8));
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn