I am using javascript in a Mirth transformer. I apologize for my ignorance but I have no javascript training and have a hard time successfully utilizing info from similar threads here or elsewhere. I am trying to trim a string from 'Room-Bed' to be just 'Bed'. The "Room" and "Bed" values will fluctuate. All associated data is coming in from an ADT interface where our client is sending both the room and bed values, separated by a hyphen, in the bed field creating unnecessary redundancy. Please help me with the code needed to produce the result of 'Bed' from the received 'Room-Bed'.
Asked
Active
Viewed 372 times
0
-
What have your already tried? What went wrong? – Carcigenicate Oct 11 '17 at 15:13
-
Strings are not mutable, you can't delete part of a string. You can make a new one though: `var newString = oldString.match(/.+\-(.+)/)[1];` – Jared Smith Oct 11 '17 at 15:13
-
1Possible duplicate of [Get everything after the dash in a string in javascript](https://stackoverflow.com/questions/573145/get-everything-after-the-dash-in-a-string-in-javascript) – William-H-M Oct 11 '17 at 15:15
1 Answers
0
There are many ways to reduce the string you have to the string you want. Which you choose will depend on your inputs and the output you want. For your simple example, all will work. But if you have strings come in with multiple hyphens, they'll render different results. They'll also have different performance characteristics. Balance the performance of it with how often it will be called, and whichever you find to be most readable.
// Turns the string in to an array, then pops the last instance out: 'Bed'!
'Room-Bed'.split('-').pop() === 'Bed'
// Looks for the given pattern,
// replacing the match with everything after the hyphen.
'Room-Bed'.replace(/.+-(.+)/, '$1') === 'Bed'
// Finds the first index of -,
// and creates a substring based on it.
'Room-Bed'.substr('Room-Bed'.indexOf('-') + 1) === 'Bed'

Dawson Toth
- 5,580
- 2
- 22
- 37