Move the string that matches the regex to the end
Topics related to client-side programming language.
Post questions and answers about JavaScript, Ajax, or jQuery codes and scripts.
-
Marius
- Posts: 107
Move the string that matches the regex to the end
I have the following problem to solve it in JavaScript:
- Find the text which is between small brackets and shift that text with the brackets to the end of the string, using regex.
Let say I have this string:
It must result:
MarPlo
Try the following code:
Code: Select all
function testToEnd(str){
//get matched string
let st = str.match(/[ ]*\([^\)]+\)[ ]*/g)
if(st){
st = st[0];
// replace the matched string and append it to end
str = str.replace(st, ' ')+st;
}
return str;
}
let str1 ='Have a (good) life';
let str2 ='Forgiveness heals the mind.';
console.log(testToEnd(str1)); // Have a life (good)
console.log(testToEnd(str2)); // Forgiveness heals the mind.
Similar Topics
-
Adding string from database into PDF
PHP - MySQL
First post
Hello Coursesweb I have a problem with my php document to convert it to PFD
I cant find out how to get results from $users into the HTML to the PDF...
Last post
Thanks MarPlo I prefer not to ask anything on stackoverflow.com
because I once typed a small i instead of I.
and received many comments about it....
-
How to escape double quote in JSON string
JavaScript - jQuery - Ajax
First post
I am trying to parse a JSON string 'Hello test' containing double quote which already escaped.
JSON.parse('{ x : Hello \ test }')
But I get...
Last post
You just need to escape the backslash \, so it turns into two backslashes \\
let obj = JSON.parse('{ x : Hello \\ test }')
console.log(obj)