1

give a string like this (it can be any pattern, but the following format):

lastname/firstname/_/country/postalCode/_/regionId/city/addressFirst/addressSecond/_/phone

I am making a function that, when I pass some address parts, the function will return those parts and remove extras part that are not requested and mantaining maximum one spacing _ if many where removed:

FE :

exs :

 input : ["country", "postalCode"]
 return "country/postalCode
 input : ["lastname", "firstname", "regionId"]
 return "lastname/firstname/_/regionId"
 input : ["firstname", "country", "regionId", "city"]
 return "firstname/_/country/_/regionId/city"
 input : ["country", "regionId", "phone"]
 return "country/_/regionId/_/phone"

Now, My method is as follow :

  type AddressPart = "firstname" | "lastname" | ... | "phone";
  const allAddressParts = ["firstname", "lastname", ... ,"phone"];
  static getAddress(
    format = "lastname/firstname/_/country/postalCode/_/regionId/city/addressFirst/addressSecond/_/phone"
    parts: AddressPart[],
  ) {
    const toRemove = allAddressParts.filter((part) => !parts.includes(part));
    toRemove.forEach((part) => {
      format = format
        .replace(`_/${part}/_`, '_')
        .replace(new RegExp(part + '/?'), '');
    });
    return format;
  }

the above looks ok, but it fail at the end and start :

_/country/postalCode/_/regionId/city/addressFirst/addressSecond/_/

How can I remove /_/ or _/ if it is situated on start or at the end without relooping over the array ?

1
  • Try this string.replace(/^_\/$/, '') Commented Dec 3, 2021 at 6:59

1 Answer 1

2

You can use regexes on the final string to remove leading and trailing unwanted symbols

The first regex looks for:

  • Start of the string, with the ^ symbol

  • One or more "_/", using

    parentheses to group the character pair,

    the + sign to allow 1 or more, and

    a backslash to escape the forward slash

And the second regex looks for:

  • a "/"
  • zero or more "_/", with the *
  • the end of the string with the $ symbol

s1 = "_/country/postalCode/_/regionId/city/addressFirst/addressSecond/_/"
s2 = s1.replace(/^(_\/)+/,"").replace(/\/(_\/)*$/,"")
console.log(s2)

You may be able to simplify

If you know for certain that there will no more than one "_" at the beginning and one at the end, you don't need the parentheses and +/* symbols.

Sign up to request clarification or add additional context in comments.

2 Comments

I made up this .match("^/?_?/?(.*?)/?_?/?$") and catch group 1, would this also work ? :x
It would work but it is not testing exactly the criteria you described. Your proposed version would also be looking for, and removing, the leading underscore in the string _underground/overground/wombling/free. For your future coding sanity it is advisable to look for and remove exactly what you know to be the extraneous text, because you will be able to recognise it when you come to edit the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.