How to trim multiple characters?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
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());
javascript string trim
add a comment |Â
up vote
6
down vote
favorite
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());
javascript string trim
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
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());
javascript string 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());
const example = ' ( some string ()() here ) ';
console.log(example.trim());
const example = ' ( some string ()() here ) ';
console.log(example.trim());
javascript string trim
javascript string trim
edited 2 hours ago
asked 2 hours ago


sertsedat
2,1141231
2,1141231
add a comment |Â
add a comment |Â
5 Answers
5
active
oldest
votes
up vote
4
down vote
accepted
You can use a regex for leading and trailing space/brackets:
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
thanks for this, but this is a bad assumption. the string inside can also contain()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
– sertsedat
2 hours ago
Well, by definitiontrim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
 |Â
show 2 more comments
up vote
3
down vote
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[ws.(.*)]+)w/g));
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
add a comment |Â
up vote
1
down vote
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
add a comment |Â
up vote
0
down vote
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));
add a comment |Â
up vote
-2
down vote
You should to replace the "(" and ")" characters as in the following example:
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
add a comment |Â
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can use a regex for leading and trailing space/brackets:
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
thanks for this, but this is a bad assumption. the string inside can also contain()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
– sertsedat
2 hours ago
Well, by definitiontrim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
 |Â
show 2 more comments
up vote
4
down vote
accepted
You can use a regex for leading and trailing space/brackets:
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
thanks for this, but this is a bad assumption. the string inside can also contain()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
– sertsedat
2 hours ago
Well, by definitiontrim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
 |Â
show 2 more comments
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can use a regex for leading and trailing space/brackets:
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
You can use a regex for leading and trailing space/brackets:
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
function grabText(str)
return str.replace(/^s+(s+(.*)s+)s+$/g,"$1");
console.log(
grabText(' ( some (string) here ) ')
)
console.log(
grabText(' ( some string ()() here ) ')
)
edited 2 hours ago
answered 2 hours ago
mplungjan
84k20120179
84k20120179
thanks for this, but this is a bad assumption. the string inside can also contain()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
– sertsedat
2 hours ago
Well, by definitiontrim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
 |Â
show 2 more comments
thanks for this, but this is a bad assumption. the string inside can also contain()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that
– sertsedat
2 hours ago
Well, by definitiontrim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.
– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
thanks for this, but this is a bad assumption. the string inside can also contain
()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that– sertsedat
2 hours ago
thanks for this, but this is a bad assumption. the string inside can also contain
()
. That's why I'm trying to trim. Just updated my question to have better clarity. Sorry about that– sertsedat
2 hours ago
Well, by definition
trim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.– vlaz
2 hours ago
Well, by definition
trim
only removes whitespace. Occasionally you'd find some trim functionality that does more - actually the other day I found out C# does that, where you can define which characters to remove from the beginning and the end but the generally understood definition of trim is for whitespace only.– vlaz
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
Please see my updated example which includes inner brackets
– mplungjan
2 hours ago
1
1
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
Well, you can write your own function that does it without regex: take parameters for what to strip, go from the beginning and end of the string and skip any strippable character, then return the inner part. But it'd be a bit of a wasted effort, as regex+replace is readily available.
– vlaz
2 hours ago
1
1
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
@sertsedat - my latest version handles both your cases
– mplungjan
2 hours ago
 |Â
show 2 more comments
up vote
3
down vote
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[ws.(.*)]+)w/g));
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
add a comment |Â
up vote
3
down vote
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[ws.(.*)]+)w/g));
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
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[ws.(.*)]+)w/g));
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[ws.(.*)]+)w/g));
const example = ' ( some (string) ()()here ) ';
console.log(example.match(/(w[ws.(.*)]+)w/g));
const example = ' ( some (string) ()()here ) ';
console.log(example.match(/(w[ws.(.*)]+)w/g));
edited 2 hours ago
answered 2 hours ago


Shubham Khatri
66.9k1376117
66.9k1376117
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
add a comment |Â
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
@mplungjan, yes that won't work. Ahh, OP updates his question
– Shubham Khatri
2 hours ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
well this also works. sorry about that. I guess you didn't see edited question. thanks for the answer
– sertsedat
1 hour ago
add a comment |Â
up vote
1
down vote
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
add a comment |Â
up vote
1
down vote
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
const str = ' ( some ( string ) here ) '.replace(/^s+(s+(.*)s+)s+$/g,'$1');
console.log(str);
edited 1 hour ago
answered 2 hours ago
khaled_bhar
114118
114118
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
add a comment |Â
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
1
1
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
Why trim after using a regex? You can let the regex do the trim too
– mplungjan
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
yes yes :) I was writing an other solution so it is fixed now
– khaled_bhar
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
Your solution is now identical to mine - exactly the same
– mplungjan
1 hour ago
add a comment |Â
up vote
0
down vote
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));
add a comment |Â
up vote
0
down vote
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));
add a comment |Â
up vote
0
down vote
up vote
0
down vote
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));
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 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 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));
edited 1 hour ago
answered 2 hours ago
Nick Parsons
1,603415
1,603415
add a comment |Â
add a comment |Â
up vote
-2
down vote
You should to replace the "(" and ")" characters as in the following example:
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
add a comment |Â
up vote
-2
down vote
You should to replace the "(" and ")" characters as in the following example:
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
add a comment |Â
up vote
-2
down vote
up vote
-2
down vote
You should to replace the "(" and ")" characters as in the following example:
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
You should to replace the "(" and ")" characters as in the following example:
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
var str = " ( Some string ) ";
str.trim();
str = str.replace("(", "");
str = str.replace(")", "");
answered 2 hours ago


Sh. Pavel
769317
769317
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52532360%2fhow-to-trim-multiple-characters%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password