简体   繁体   English

如何使用JavaScript在其他特定字符之前插入字符

[英]How to insert character before another specific character using javascript

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" . 我正在使用javascript的eval进行乘法,例如eval("2(3)") ,但出现错误"unexpected input value"

To solve this I need the following: 为了解决这个问题,我需要以下几点:

var str = "2(3)";

to be changed to this: 更改为:

output = "2*(3)";

You can use String.prototype.replace() . 您可以使用String.prototype.replace()

Example: 例:

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

That will output: 2*(3) 将会输出: 2*(3)

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: 如果要执行更复杂/嵌套的乘法,则需要使用带有replace() (\\)|\\d+(?!\\d)\\.?)(?=[(\\d.+-])更复杂正则表达式(\\)|\\d+(?!\\d)\\.?)(?=[(\\d.+-]) replace()函数,适用于嵌套括号,加号,负数以及以小数点开头或结尾的数字的任意组合:

 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); 

You can use the replace() function combined with a little regex ([0-9])\\(([0-9]) : 您可以将replace()函数与少许正则表达式([0-9])\\(([0-9])

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

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) 

Try this: 尝试这个:

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

Works good for me :) 对我有用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在一个字符之前插入一个字符? - How to insert one character before another? 在字符串中最后一次出现特定字符之前插入 - Insert before the last occurrence of a specific character in a string 如何在JavaScript中特定字符之前获取字符串? - How to get the string just before specific character in JavaScript? javascript在特定字符之前删除字符串 - javascript remove string before a specific character 特定字符前的Javascript修剪值 - Javascript trim value before a specific character 如何在字符串中的每个特定字符(如“(”)中插入字符-javascript - How can i insert a character in every specific character like “(” inside my string - javascript 在字符串javascript中每次出现另一个字符时插入字符 - Insert character at every occurrence of another character in a string javascript 如何使用javascript或者在空格之前检索@字符之后的字符串? - How to retrieve string after @ character and before space using javascript or react? 如何在JavaScript中使用正则表达式在数字和字符/字符串之间插入空格 - How to insert space between a number and a character /string using regex in javascript 如何使用javascript在光标位置之前和@字符之后提取字符串直到第一个空格字符? - How to extract string before cursor position and after the @ character until the first whitespace character using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM