RegExp 将逆三角函数(例如 sin^(-1)(x))转换为适当的 notaion(例如 asin(x))



我有一个字符串:

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%;}

相关内容

最新更新