How to remove character from inside a braces in jquery ? [Resolved]

Posted by Nandkishorre under jQuery on 4/6/2016 | Points: 10 | Views : 2443 | Status : [Member] | Replies : 4
Hi team,
i have data inside a square brackets. i need to remove character in side a braces.

ex:
(a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&,+/]


as above example i need to remove comma(,) inside a square brackets.

i need output as below format.

(a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&+/]


how to solve this using jquery or javascript.. ?




Regards
Nanda Kishore.CH




Responses

Posted by: Rajnilari2015 on: 7/9/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this

<html>

<head>
<script type='text/javascript'>

function test()
{
var input = '(a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&,+/]'; //input string
var output = input.replace(/\[.*?\]/g, function(match) {
return match.replace(/,/g, '');
});
console.log(output);
}

</script>

</head>
<body>

<button id="btn1" onClick="test()">Test</button>
</div>
</body>
</html>



--
Thanks & Regards,
RNA Team

Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mailtodhanz on: 7/8/2016 [Member] Starter | Points: 25

Up
0
Down
I hope below code will resolve your issue

var s = '(a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&,+/]';

alert(s.replace(/\[[^\]]*\]/g, function(x){return x.replace(/,/g, '')}))


Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 7/8/2016 [Member] [MVP] Silver | Points: 25

Up
0
Down
As mentioned already you best option is to go with regex. You can find an alternative regex approach provided below

function removecommas(source){

//Build your expression
var regexexp = new RegExp(",(?![^\\]]*(?:\\[|$))");
//Test your current value
alert(source.replace(regexexp,''));
}


Complete Code
<!DOCTYPE html>

<html>
<head>
<meta charset=utf-8 />
<script>
function removecommas(source){
//Build your expression
var regexexp = new RegExp(",(?![^\\]]*(?:\\[|$))");
//Test your current value
alert(source.replace(regexexp,''));
}
</script>
</head>
<body>
<pre>Remove commas in between square brackets</pre>
<input id='YourElement' value='(a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&,+/]' /><button onclick='removecommas(YourElement.value);'>Remove</button>
</body>
</html>


You can find a demo here :http://jsbin.com/rolanakobu/edit?html,output

Thanks,
A2H
My Blog

Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amatya on: 4/6/2016 [Member] Silver | Points: 25

Up
-1
Down
 parseFloat(' (a-d){3-5},(A-F){5-8},(0-6){8-9},[#$%^&,+/] '.replace(/,/g, ''));


Try above one. Hope it helps

Thanks

Feel free to share informations.
mail Id ' adityagupta200@gmail.com
Thanks

Nandkishorre, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response