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
-
Make regex replace() work on all matches
JavaScript - jQuery - Ajax
First post
I’m trying to replace all spaces within a string with hyphens.
I tried this:
let str ='This is my text';
str = str.replace(/\s/, '-');...
Last post
Add the global search flag (/g ) to your regex to match all occurrences.
let str ='This is my text';
str = str.replace(/\s/g, '-');...
-
Remove backslash from string
PHP - MySQL
First post
How can I remove backslashes from string in php?
I tried the following code.
$str ='abc-\123';
$str = stripcslashes($str);
echo $str; //...
Last post
You can use str_ireplace() to remove backslash from string in php, but like in the following code (add two backslashes into the removing argument):...
-
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....
-
Check for an array of words in a string in php
PHP - MySQL
First post
I have a list of spam words that are into an array. When a user submits a string text, I want to know if it contains these words. How can I do this...
Last post
You could add the spam words into a string, with | as word separator and then use regular expression to check.
$my_words =...
-
Extract number and periods from string in php
PHP - MySQL
First post
I have a string with numbers and periods. For example '123456...'
I want to separate 123456 and ... and still get the number and the period in php....
Last post
It's because the dot (.) in regex is any character, while \. is literal dot.
Try the following code:
$matches = null;
$input = '123456...';...