0

I need to know how to insert an asterix before an open bracket.

I am doing multiplication using javascript's eval, for example, eval("2(3)"), but I am getting the error "unexpected input value".

To solve this I need the following:

var str = "2(3)";

to be changed to this:

output = "2*(3)";

5 Answers 5

3

You can use String.prototype.replace().

Example:

var str = '2(3)'
var output = str.replace(/\(/g, '*(')
console.log(output)

That will output: 2*(3)

Sign up to request clarification or add additional context in comments.

4 Comments

Congratulations on your first post. A good tip when answering javascript/HTML/CSS questions is to use a code snippet. Not only does it allow others to test the code, but it allows you to debug it before posting. (Unfortunately, editing snippets isn't supported well/at all for mobile devices.) Apart from my answer ;), yours is the next best one as it works for multiple values and negative numbers as well. It is also the simplest.
Thanks @robinCTS !
I appreciate the thanks. Try not to use "thanks" only comments too often, though. See this FAQ post for further info. The usual way to say thanks on StackOverflow/Stack Exchange sites is to upvote comments, accept answers, and when you have at least 15 rep, to upvote answers/questions. For exceptional answers you can also award a bounty (min 75 rep required).
Also note that whilst, technically, trailing semi-colons are not required in javascript, it is a very good standard practice to always include them.
2

If you want to perform more complex/nested multiplication you need to use this more complicated regex (\)|\d+(?!\d)\.?)(?=[(\d.+-]) with the replace() function. It will work for any combination of nested brackets, as well as plus signs, negative numbers and numbers starting or ending with a decimal point:

var str, output;
str = '2(3)';
output = str.replace(/(\)|\d+(?!\d)\.?)(?=[(\d.+-])/g, '$1*');
console.log(output);
str = '2(3)(-4)';
output = str.replace(/(\)|\d+(?!\d)\.?)(?=[(\d.+-])/g, '$1*');
console.log(output);
str = '((.22(33))((-44)-55.(66)+77))';
output = str.replace(/(\)|\d+(?!\d)\.?)(?=[(\d.+-])/g, '$1*');
console.log(output);

3 Comments

Dear robinCTS, I like to learn regex but was confuse while learning regex. Do you have any nice tutorial to easily learn regex. thanks
@Vijaykarthik The site I learned about regexes, and still use if I need to check something, is regular-expressions.info/tutorial.html. Another useful site is regex101. There you can write, debug and learn regexes all at the same time.
Yes sure, useful link. Thanks @robinCTS
1

You can use the replace() function combined with a little regex ([0-9])\(([0-9]) :

var str = "2(3)";
var output = str.replace(/([0-9])\(([0-9])/g,'$1*($2');
console.log(output);

2 Comments

Thanks for your reply, your answer will suit for constant value, but if the value getting dynamically how we can proceed For example : var str = "2(3)(3)"; or var str = "2(3)3";
Zenoo & @Vijaykarthik Plus this answer doesn't work for negative numbers.
0

You can slice the string and add the asterisk sign like this:

var str = "2(3)";
var new_str = str.slice(0, str.indexOf("(")) + "*" + str.slice(str.indexOf("("));
console.log(new_str)

Comments

0

Try this:

var str = "2(3)";
var a = str.split('');
a.splice(1,0,"*");
a = a.join(''); 
console.log(a);

Works good for me :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.