Here I'm getting the output but if a string is greater than 15 characters it should be split.
const a = "/qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg /sub node 01"
expected output = "Qar reg qar reg.../Sub node 01"
const a = "/_dept/jtyut"
expected output = "Dept/Jtyut"
const path = e.toLowerCase().replace('_', '');
return path.replace(/(?:^|[\s-/])\w/g, function (match) {
return match.toUpperCase();
})
Not the most efficient solution, but this method is effective and easy to read:
function CleanPath(p){ return p.split("/") // Split on /.filter(i => i.length > 0) // Remove empty elements.map(i => i.replace(/^_/, '')) // Remove leading underscores.map(i => i.length > 15? i.substr(0, 15) + "...": i) // Limit to 15 characters +... .map(i => i.charAt(0).toUpperCase() + i.slice(1)) // Uppercase leading character.join("/"); // Join on / } console.log(CleanPath("/qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg /sub node 01")); console.log(CleanPath("/_dept/jtyut"));
If you prefer you can merge all the map
calls into one:
function CleanPath(p){ return p.split("/") // Split on /.filter(i => i.length > 0) // Remove empty elements.map(i => { i = i.replace(/^_/, ''); // Remove leading underscores i = i.length > 15? i.substr(0, 15) + "...": i; // Limit to 15 characters +... return i.charAt(0).toUpperCase() + i.slice(1); // Uppercase leading character }).join("/"); // Join on / } console.log(CleanPath("/qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg /sub node 01")); console.log(CleanPath("/_dept/jtyut"));
And if you need IE support you can replace the arrow functions with regular function bodies:
function CleanPath(p){ return p.split("/") // Split on /.filter(function(i){ return i.length > 0; }) // Remove empty elements.map(function(i){ i = i.replace(/^_/, ''); // Remove leading underscores i = i.length > 15? i.substr(0, 15) + "...": i; // Limit to 15 characters +... return i.charAt(0).toUpperCase() + i.slice(1); // Uppercase leading character }).join("/"); // Join on / } console.log(CleanPath("/qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg qar reg /sub node 01")); console.log(CleanPath("/_dept/jtyut"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.