Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Finding last ( in the string
Message
 
 
To
18/07/2017 17:07:05
General information
Forum:
Javascript
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
01652707
Message ID:
01652710
Views:
38
Hi Antonio,

Sorry, I didn't see your response earlier. I wrote the following:
/**
     * 
     * @param {any} fullTitle
     * @returns object
     */
    var splitFullTitle = function (fullTitle) {
        log.debug(fullTitle);

        var indexOf = fullTitle.lastIndexOf("(");
        var description = fullTitle;
        var nickname = "";

        if (indexOf > 0) {
            description = fullTitle.substr(0, indexOf - 1);
            var lastIndex = fullTitle.lastIndexOf(")");
            nickname = fullTitle.substring(indexOf + 1, lastIndex);
        }

        return { description: description, nickname: nickname };
    };
and use it as
var item = splitFullTitle(data.departments[0].items[i].title);                                    

                                    allDepartments.push({
                                        "id": data.departments[0].items[i].intKeys[0],
                                        "descrip": item.description,
                                        "nickname": item.nickname,
                                        "preventDefault": true,
                                        "hash": data.departments[0].items[i].keys[0],
                                        "hidden": data.departments[0].items[i].hidden
                                    });
Do you think your version is better and more optimized or should I leave the code I already put in place?

Thanks again.


>
>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.
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform