How to remove text from a string

English main discussion
Post Reply
  • Author
  • Message
Offline
Posts: 8
Joined: Sun Nov 13, 2011 7:02 pm
Location: Dayton, Ohio, USA

How to remove text from a string

Post 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.

KDJ
Offline
Posts: 1949
Joined: Sat Mar 06, 2010 7:40 pm
Location: Poland

Re: How to remove text from a string

Post by KDJ »

Dellji wrote:...
var NoNickName = "didn't work"
NoNickname = NickName.replace(/ *\([^)]*\) */g, "");
...
These are two different variables.

Offline
Posts: 8
Joined: Sun Nov 13, 2011 7:02 pm
Location: Dayton, Ohio, USA

Re: How to remove text from a string

Post 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

YuS
Offline
Posts: 513
Joined: Sun Sep 15, 2013 8:25 am
Location: 013 в Тентуре, семь по Спирали, налево от Большой Медведицы

Re: How to remove text from a string

Post 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

Offline
Posts: 8
Joined: Sun Nov 13, 2011 7:02 pm
Location: Dayton, Ohio, USA

Re: How to remove text from a string

Post 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
Post Reply