0

In javascript

I can detect a character in a string and then write a letter before that character?

18th Feb 2021, 2:15 AM
JoseD
JoseD - avatar
15 Réponses
+ 2
// did you know regular expressions? string = 'Heo world'; string = string.replace(/(?<=e)(?=o)/,'ll'); console.log(string);
18th Feb 2021, 3:48 PM
visph
visph - avatar
+ 1
Ipang i have a text for example "heo word" i want to write "ll" between the letter "e" and "o" for later eval with javascript eval()
18th Feb 2021, 3:41 PM
JoseD
JoseD - avatar
+ 1
visph thanks
18th Feb 2021, 4:42 PM
JoseD
JoseD - avatar
0
What character to detect? And where you write the letter? Might help clear things up if you can provide an example.
18th Feb 2021, 3:33 PM
Ipang
0
Ipang visph I'm not going to use it specifically with text, it is for mathematical expressions and that every time in the string there is a word before it, "Math." then use "eval ()" (Translate by google )
18th Feb 2021, 3:57 PM
JoseD
JoseD - avatar
0
share your code to get more accurate answer ;)
18th Feb 2021, 3:58 PM
visph
visph - avatar
18th Feb 2021, 4:00 PM
JoseD
JoseD - avatar
0
You mean you read a string, for example "Math. 9 x 2 ÷ 2" Then you want the string to become "9 * 2 / 2" In order to be processed by eval()
18th Feb 2021, 4:00 PM
Ipang
0
For example Text: cosh(5) -> Math.cosh(5) Eval()
18th Feb 2021, 4:03 PM
JoseD
JoseD - avatar
0
why don't you stuck at 'Math.'+string?
18th Feb 2021, 4:04 PM
visph
visph - avatar
0
visph ? eval("Math."+string) ?
18th Feb 2021, 4:06 PM
JoseD
JoseD - avatar
0
just as you already do ^^
18th Feb 2021, 4:07 PM
visph
visph - avatar
0
visph could you explain me better Sorry I'll be back in 2 hours I'm busy
18th Feb 2021, 4:14 PM
JoseD
JoseD - avatar
0
You can make a list of function names e.g. [ "cos", "sin", "pow" ]. Then do a find & replace operation on the string before passing the string to eval(). let names = [ "sin", "cos", "tan" ]; for( let name of names ) { // if string contains <name> // replace it with "Math." + <name> }
18th Feb 2021, 4:15 PM
Ipang