我有一个字符串:
sin^(-1)(x)*cos^(-1)(x)
我想从它转换为:
asin(x)*acos(x)
考虑到正则表达式中的符号^
很特殊,我如何使用javascript RegExp执行此操作?
如果需要将反三角函数转换为正确的符号,则可以这样做:
const str = `sin^(-1)(x)*cos^(-1)(x)/tan^(-1)(x^3)*ctg^(-1)(x^2)`,
functions = ['sin', 'cos', 'tan', 'ctg'],
regExp = new RegExp(`(${functions.join('|')})\^\(-1\)`, 'g')
result = str.replace(regExp, "a$1")
console.log(result)
.as-console-wrapper{min-height:100%;}