Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Finding last ( in the string
Message
From
18/07/2017 17:07:05
 
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01652707
Message ID:
01652709
Views:
66
This message has been marked as a message which has helped to the initial question of the thread.
function splitName (fullName)
{
  if (parts = fullName.match(/(.*)\((.+)\)\s*$/, "g")){
    this.description = parts[1];
    this.nickname = parts[2];
  } else {
    this.description = fullName;
    this.nickname = "";
  }
}

sn = new splitName("Some String (here)");

// sn.description = "Some String"
// sn.nickname = "here"

sn = new splitName("Some (String) (here)");

// sn.description = "Some (String)"
// sn.nickname = "here"

sn = new splitName("Some String here");

// sn.description = "Some String here"
// sn.nickname = ""
>UPDATE.
>
>Apparently a similar code is used in several places.
>
>So, I have a question - if I want to return two strings as a result, would this solution work
>
>function splitName (fullName)
>{
>  var item = {description : partOne, nickname : partTwo };
>
>  return item;
>}
>
>
>and use it as
>
>var splittedName  = splitName ("Some String (here)")
>
>var firsPart = splittedName.description;
>var secondPart = splittedName.nickname;
>
>
>
>Hi everybody,
>
>My colleague wrote the following code
>
>
> for (var i = 0; i < data.departments[0].items.length; i++) {
>                                    var temp = data.departments[0].items[i].title.split("(");
>                                    var description = temp[0];
>                                    var nickname = temp[1].split(")")[0];
>
>                                    allDepartments.push({
>                                        "id": data.departments[0].items[i].intKeys[0],
>                                        "descrip": description,
>                                        "nickname": nickname,
>                                        "preventDefault": true,
>                                        "hash": data.departments[0].items[i].keys[0],
>                                        "hidden": data.departments[0].items[i].hidden
>                                    });
>                                }
>
>which is failing when description itself has () in it.
>
>So, how can I adjust the var description and var nickname lines above to get information before the very last ( in the string. E.g. nickname is in the () at the end of the string and description may include more () inside. I guess we should not use split function, but may be reverse and indexof?
>
>Thanks in advance.
>
>UPDATE. Found this
>
>https://www.w3schools.com/jsref/jsref_lastindexof.asp
>
>Looks like I may use that.
----------------------------------
António Tavares Lopes
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform