2

I want to replace the last word of x with y if y includes the last word of x. How can I manage that?

let x ='jaguar lion tiger panda'
let y = 'pandas'

expected result:

'jaguar lion tiger pandas'

if

y = 'cat'

Expected result:

'jaguar lion tiger panda cat'

Code I've tried:

console.log(response)
let before = this.text.split(' ')
console.log(before)
console.log(before.length)
let a = before.slice(before.length-1)
console.log(a)
if (response.data.text[0].includes(a)) {
  let x = (before.slice(0, before.length-1))
  let y = x.replace(',', ' ')
  this.preResult = y.push(response.data.text[0])
} else {
  this.preResult.push(this.text + ' ' + response.data.text[0])

3 Answers 3

3

You can use a regular expression to match the last word, and then carry out your test by checking whether the y includes the word or not. If so, replace the word with y, else replace with the original word concatenated with y:

const x ='jaguar lion tiger panda'

const doReplace = y => x.replace(
  /\S+$/, // match non-space characters, followed by the end of the string
  (word) => (
    y.includes(word)
    ? y
    : word + ' ' + y
  )
);
console.log(doReplace('pandas'));
console.log(doReplace('cat'));

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

8 Comments

Regular expressions are a very powerful and concise tool for string matching and manipulation. They're quite useful for programmers to know. The one used here is super simple, though - \S+ matches non-whitespace characters, and $ matches the end of the string.
and I need to replace panda with pandas because its included in pandas. can you do that also?
The code in the answer does that because of the .includes test - press "Run code snippet" to see.
ok thanks 1 last thing i am civil engineer and studying architecture but i love coding at least i think so but its so hard so i turned to design web pages which is easy and fun but i really want to have a good understanding of what happens in background but i dont want to spend so much time on things that are worthless what can you recommend so i research a bit and decide to do or not.. captain
It's a really broad field - it's quite opinion-based, so it's up to you.
|
0

Another solution:

    let x ='jaguar lion tiger panda'
    let y = 'pandas'
    let splited = x.split(' ')
    let lastWord = splited[splited.length - 1]
    
    if(y.indexOf(lastWord) >= 0){
        splited[splited.length - 1] = y
    }else{
        splited.push(y)
    }
    
    let result = splited.join(' ')
    console.log(result)

1 Comment

I am trying to understand this. this seem to work thanks.
0
function replaceLastWord(x, y) {
    let result = x.split(' '), lastIndex = (result.length || 1) - 1, lastWord = result[lastIndex]
    result[lastIndex] = y.indexOf(lastWord) !== -1 ?  y : `${lastWord} ${y}`
    return result.join(' ')
}

console.log(replaceLastWord('jaguar lion tiger panda', 'pandas'))
console.log(replaceLastWord('jaguar lion tiger panda', 'cat'))
console.log(replaceLastWord('', 'pandas'))

Comments

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.