Nomi
Nomi

Reputation: 23

What is the regex to remove last + sign from a string

I'm trying to generate a link using jQuery and need to trim the last '+' sign off the end. Is there a way to detect if there is one there, and then trim it off?

So far the code removes the word 'hotel' and replaces spaces with '+', I think I just need another replace for the '+' that shows up sometimes but not sure how to be super specific with it.

var nameSearch = name.replace("Hotel", "");
nameSearch = nameSearch.replace(/ /g, "+");

Upvotes: 2

Views: 116

Answers (3)

F. Müller
F. Müller

Reputation: 4062

As an alternative to mplungjan's answer, you can use str.endsWith() for the check. If it ends on the + it will be cut out. There is no need for regex. If you can avoid regex you definitely should.

let str = "Hotel+";
if (str.endsWith("+")) {
  str = str.substr(0, str.length - 1);
}
console.log(str);

Below you can find a function to replace all the whitespace characters with + excluding the last one:

const raw = "My Ho te l ";

function replaceSpacesWithPlus(raw) {
  let rawArray = Array.from(raw);
  let replArray = [];
  for (let i = 0; i < rawArray.length; i++) {
    const char = rawArray[i];
    // handle characters 0 to n-1
    if (i < rawArray.length - 1) {
      if (char === ' ') {
        replArray.push('+');
      } else {
        replArray.push(char);
      }
    } else {
      // handle last char
      if (char !== ' ' && char !== '+') {
        replArray.push(char);
      }
    }
  }
  return replArray;
}

console.log(replaceSpacesWithPlus(raw));

Upvotes: 3

mplungjan
mplungjan

Reputation: 178350

The answer to

What is the regex to remove last + sign from a string

is this

const str = "Hotel+"
const re = /\+$/; // remove the last plus if present. $ is "end of string"
console.log(str.replace(re,""))

The question is however if this is answering the actual problem at hand

If you have the string

"Ritz Hotel"

and you want to have

https://www.ritz.com

then you could trim the string:

const fullName = "Ritz Hotel",
  name = fullName.replace("Hotel", "").trim().toLowerCase(),
  link = `https://www.${name}.com`;

console.log(link)

// or if you want spaces to be converted in url safe format

const fullName1 = "The Ritz Hotel",
  name1 = fullName1.replace("Hotel", "").trim().toLowerCase(),
  link1 = new URL(`https://www.hotels.com/search?${name1}`).toString()

console.log(link1)

Upvotes: 6

Abhisek Dutta
Abhisek Dutta

Reputation: 257

The below snippet will remove all the existing + symbols from string.

let str = 'abcd + efg + hij';
str = str.replace(/\+/gm, '');
//output: abcd  efg  hij

For trim use the below snippet. It will remove the spaces from around the string.

let str = "   Hello    World!!   "
str = str.trim();
// output: Hello    World!!

If you want to replace the last + symbol only.

let str = 'abcd + efg + hij';
let lastIndex = str.lastIndexOf('+');

if (lastIndex > -1) {
    let nextString = str.split('');
    nextString.splice(lastIndex, 1, '');
    str = nextString.join('');
}
// output: abcd + efg  hij

Upvotes: 0

Related Questions