regex - replacement of string using regular expression at Xth position intervals (javascript) - Stack Overflow

I would very much appreciate some assistance from the munity on replacing a string at xth position inte

I would very much appreciate some assistance from the munity on replacing a string at xth position intervals, using javascript regex. For example, if the string length is 161 and the replacement text is <br />, regex would replace the string at the 40th, 80th, 120th, and 160th positions with this replacement text. Is this possible using regex?

Thank you very much.

I would very much appreciate some assistance from the munity on replacing a string at xth position intervals, using javascript regex. For example, if the string length is 161 and the replacement text is <br />, regex would replace the string at the 40th, 80th, 120th, and 160th positions with this replacement text. Is this possible using regex?

Thank you very much.

Share edited Oct 28, 2011 at 14:52 user717236 asked Oct 28, 2011 at 14:49 user717236user717236 5,03920 gold badges67 silver badges102 bronze badges 4
  • 1 While it may be possible with regex, why not use other techniques to do this? It seems to me that you want to insert something every (n % 40 == 0) position. – FailedDev Commented Oct 28, 2011 at 14:53
  • Yes, that is correct. Why not other techniques? Because my logic thinks regex would require less lines of code and could be the simplest. I'm open to other techniques, however. – user717236 Commented Oct 28, 2011 at 14:54
  • 1 Less lines of code doesn't necessarily make it neither simpler nor more efficient. Just a thing to note. – FailedDev Commented Oct 28, 2011 at 14:59
  • Yes, thank you, I appreciate your advice. Note: That's why I said 'could' and not 'will' in my above ment. – user717236 Commented Oct 28, 2011 at 15:03
Add a ment  | 

2 Answers 2

Reset to default 4

A method to add <br /> at ever 40th position is by using the following line:

string = string.replace(/([\S\s]{40})/g , "$1<br />");

If you want to dynamically set the position use:

var positions = 40;
var pattern = new RegExp("([\\s\\s]{" + positions + "})", "g");
string = string.replace(pattern , "$1<br />");

Explanation of the code:

  1. The first argument of the replace function is a RegExp:
    • ([\S\s] = all non-whitespace and white-space characters = every character).
    • {40} = 40 characters
    • The g flag means: global match, ie: match every possible occurence
    • The parentheses inside the RegExp means: Create a group. This group can later be referred by $1 (first group)
  2. The second argument of the replace function contains $1<br />. That is: replace the full match by the first group ($1), and add <br /> to it.
var str = "12345678901234567890";
var newStr = str.replace(/(.{5})/g,"$1<br/>");

for every 40, change the 5 to 40.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744808574a4594926.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信