-2

I have a string lets say,

const message = 'This is a relationship: Salary = 73010 - 58.9 * Potential'

What I am trying to achieve here is get everything after relationship: and put it in a seperate variable using Regex. After storing the new string in a seperate variable I want the old variable containing the whole string to be replaced with just

message = 'This is a relationship'

So far, I have managed to do this:

const equation = new RegExp(/relationship:.*$/); // this gets everything removed starting from the relationship:
const tooltip = message.split(equation);
const tip = message.replace(equation, '');

Apologies for my noob code. I just started understanding Regex!

Nutella43
  • 31
  • 5

1 Answers1

0

Is the pattern always everything after :? If so this is quite simple, you don't even need an explicit regex:

let message = 'This is a relationship : Salary = 73010 - 58.9 * Potential';

const array = message.split(":");

message = array[0].trim(); // Overwrite the original message (you asked for this)
const tooltip = array[1].trim();

console.log(`message: "${ message }"`);
console.log(`tooltip: "${ tooltip }"`);
.as-console-wrapper { min-height: 100%!important; top: 0; }

This returns:

message: "This is a relationship"
tooltip: "Salary = 73010 - 58.9 * Potential"
Peter Seliger
  • 11,747
  • 3
  • 28
  • 37
Telmo Trooper
  • 4,993
  • 1
  • 30
  • 35