Home how to pass variable inside replace java-script function
Reply: 0

how to pass variable inside replace java-script function

user1054
1#
user1054 Published in May 24, 2018, 4:00 pm

This question already has an answer here:

  • How do you use a variable in a regular expression? 16 answers

I'm using the below code to trim the string based on the condition like 15 characters per line, in this below code, 15 was hard-coded.But I need to make it dynamic like defining variable and pass into replace function.

var str = "Here's to your next step.Keep Walking.Keep Watching"
result = str.replace(/(.{1,15})(?:\n|$| )/g, "$1|\n");
console.log(result);

How I need

var trim_val =15;
var str = "Here's to your next step.Keep Walking.Keep Watching"
result = str.replace(/(.{1,trim_val})(?:\n|$| )/g, "$1|\n"); //here i have to pass that variable
console.log(result);

Thanks in Advance

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.351195 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO