I have a string as follows
const example = ' ( some string ()() here ) ';
If I trim the string with
example.trim()
it will give me the output: ( some string ()() here )
But I want the output some string ()() here
. How to achieve that?
const example = ' ( some string ()() here ) ';
console.log(example.trim());
I have a string as follows
const example = ' ( some string ()() here ) ';
If I trim the string with
example.trim()
it will give me the output: ( some string ()() here )
But I want the output some string ()() here
. How to achieve that?
const example = ' ( some string ()() here ) ';
console.log(example.trim());
Share
Improve this question
edited May 7, 2019 at 19:44
sertsedat
asked Sep 27, 2018 at 8:10
sertsedatsertsedat
3,6001 gold badge27 silver badges45 bronze badges
5 Answers
Reset to default 6You can use a regex for leading and trailing space/brackets:
/^\s+\(\s+(.*)\s+\)\s+$/g
function grabText(str) {
return str.replace(/^\s+\(\s+(.*)\s+\)\s+$/g,"$1");
}
var strings = [
' ( some (string) here ) ',
' ( some string ()() here ) '];
strings.forEach(function(str) {
console.log('>'+str+'<')
console.log('>'+grabText(str)+'<')
console.log('-------')
})
If the strings are optionally leading and/or trailing, you need to create some optional non-capturing groups
/^(?:\s+\(\s+?)?(.*?)(?:\s+\)\s+?)?$/g
/^ - from start
(?:\s+\(\s+?)? - 0 or more non-capturing occurrences of ' ( '
(.*?) - this is the text we want
(?:\s+\)\s+?)? - 0 or more non-capturing occurrences of ' ) '
$/ - till end
g - global flag is not really used here
function grabText(str) {
return str.replace(/^(?:\s+\(\s+?)?(.*?)(?:\s+\)\s+?)?$/g, "$1");
}
strings = ['some (trailing) here ) ',
' ( some embedded () plus leading and trailing brakets here ) ',
' ( some leading and embedded ()() here'
];
strings.forEach(function(str) {
console.log('>' + str + '<')
console.log('>' + grabText(str) + '<')
console.log('-------')
})
You can use the regex to get the matched string, The below regex, matches the first character followed by characters or whitespaces and ending with a alphanumberic character
const example = ' ( some (string) ()()here ) ';
console.log(example.match(/(\w[\w\s.(.*)]+)\w/g));
If you want to remove only the outer brackets after the trim you can use
var input = ' ( some string ()() here ) '.trim();
if( input.charAt(0) == '(' && input.charAt(input.length-1) == ')') {
var result = input.slice(1, -1).trim()
console.log(result)
}
Last trim is optional which removes the space between (
and s
also between e
and )
You could use a recursive method and specify the number of times you wish to trim the string. This will also work with things other than round brackets, for instance, square brackets:
const example = ' ( some string ()() here ) ';
const exampleTwo = ' [ This, is [some] text ] ';
function trim_factor(str, times) {
if(times == 0) {
return str;
}
str = str.trim();
return str.charAt(0) + trim_factor(str.substr(1, str.length-2), times-1) + str.charAt(str.length-1);
}
console.log(trim_factor(example, 2));
console.log(trim_factor(exampleTwo, 2));
const str = ' ( some ( string ) here ) '.replace(/^\s+\(\s+(.*)\s+\)\s+$/g,'$1');
console.log(str);