Page 1 of 1

How to remove text from a string

Posted: Fri Jan 18, 2019 2:38 pm
by Dellji
I can't get this code to work


var NickName = "Hello, this is Mike (example) xx "

var NoNickName = "didn't work"
NoNickname = NickName.replace(/ *\([^)]*\) */g, "");

AkelPad.MessageBox(0, NoNickName + " " + NickName, "Remove NickNames", 64 /*MB_ICONINFORMATION*/);

// Result should be
// "Hello, this is Mike"

// Here, we are using regular expression / *\([^)]*\) */g that finds the characters inside brackets (in this case small round brackets) and replace them with empty string.

Re: How to remove text from a string

Posted: Sat Jan 19, 2019 8:18 pm
by KDJ
Dellji wrote:...
var NoNickName = "didn't work"
NoNickname = NickName.replace(/ *\([^)]*\) */g, "");
...
These are two different variables.

Re: How to remove text from a string

Posted: Sun Jan 20, 2019 12:15 am
by Dellji
Your revised code didn't work either.

Also sorry for not giving a clearer example

So here is the proper example:
var NickName = "Hello, this is Michael (Mike) from Ohio"

The result should be

Hello, this is Michael from Ohio

Only the parenthesis “()” and “Mike” should be removed and the two substrings concatenated

The code works in javascript in browser

Re: How to remove text from a string

Posted: Sun Jan 20, 2019 5:24 am
by YuS
Dellji wrote: The result should be

Hello, this is Michael from Ohio

Code: Select all

NickName = "Hello, this is Michael (Mike) from Ohio"
NoNickName = NickName.replace(/ *\([^)]*\) */g," ")
WScript.Echo(NoNickName)
Image

Re: How to remove text from a string

Posted: Sun Jan 20, 2019 2:07 pm
by Dellji
YuS wrote:
Dellji wrote: The result should be

Hello, this is Michael from Ohio

Code: Select all

NickName = "Hello, this is Michael (Mike) from Ohio"
NoNickName = NickName.replace(/ *\([^)]*\) */g," ")
WScript.Echo(NoNickName)
Image
Thanks that worked