0

(function() {

    function takeRecordingAway() {
        try {
            let $title = document.querySelector('.progTitle');
            let splitInfo = $title.textContent.split(' ');
            let lastInd = splitInfo.lastIndexOf('Recording');
            let filterResult = splitInfo.slice(lastInd, splitInfo.length).join('');
            let finalResult = splitInfo.join('').replace(filterResult, '');
            $title.textContent = finalResult;
        } catch(e) {
            console.log("Error", e);
        }
        
    }
    
    function init() {
        takeRecordingAway();
    }
    init();
})();
<div class="progTitle">Advisory Round: Key Steps for StartupsRecording is not available for this program</div>

I am trying to remove a phrase from a string. The phrase I want to remove is 'Recording is not available for this program' from this string:

'Advisory Round: Key Steps for StartupsRecording is not available for this program'

How would I be able to find this phrase and remove it?

Currently I am using this function that is not exactly working:

(function() {

function takeRecordingAway() {
    try {
        let $title = document.querySelector('.progTitle'); //div containing the phrase
        let splitInfo = $title.textContent.split(' ');
        let lastInd = splitInfo.lastIndexOf('Recording');
        let filterResult = splitInfo.slice(lastInd, splitInfo.length).join('');
        let finalResult = splitInfo.join('').replace(filterResult, '');
        $title.textContent = finalResult;
    } catch(e) {
        console.log("Error", e);
    }
    
}

function init() {
    takeRecordingAway();
}
init();
})();

2 Answers 2

4

Replacing the textContent of the title with a filtered textContent value using String.prototype.replace() would do the trick.

function removePhraseFromTitle(phraseToFind) {
  const title = document.querySelector('.progTitle');
  title.textContent = title.textContent.replace(phraseToFind, '');
}

removePhraseFromTitle('Recording is not available for this program');
<div class="progTitle">Advisory Round: Key Steps for StartupsRecording is not available for this program</div>

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

Comments

0

I would use a regular expression that captures the said phrase in this scenario. Then use String.prototype.replace() to remove it.

function removePhrase(str) {
    const re = /(Recording is not available for this program)/g;
    return re.replace(re, '');
}

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.