String Replace

Hi I’m trying to replace:




I’ve tried everything I can find on Google but nothing so far has worked.

I thought it would be simple!

Any help?

Thank you

var string = 'Economy1';
var newstring = string.replace(/1/, '');

Hi there Freejoy,


(function( d ) {
   'use strict';
   var word = 'Economy1', new_word;
       new_word= word.replace( word.charAt( word.length - 1 ), '' );
       console.log( new_word );
}( document ));

…will replace any character at the end of the string. :winky:


I tried both sugestions but neither of them worked. : )

Well, show us your actual coding attempts which
enables you to make such a sweeping statement. :biggrin:


Or if you just want all but the last character, you can use the slice method.

var result = "abc9".slice(null, -1) // "abc"
1 Like

Hi there Freejoy,

here is a working example- using @Paul_Wilkins’ short method…

<html lang="en">

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>untitled document</title>

<!--<link rel="stylesheet" href="screen.css" media="screen">-->

<style media="screen">
body {
    background-color: #f9f9f9;
    font: normal 1em / 1.5em BlinkMacSystemFont, -apple-system, 'Segoe UI', roboto, helvetica, arial, sans-serif;





(function( d ) {
   'use strict';
   var el = d.querySelector( 'h1' ), word, new_word
       d.querySelector( 'button' ).addEventListener( 'click', 
          function() {
                   word = el.textContent;
             if  ( word !== new_word) {
                   new_word = word.slice( null, -1 );
                   el.textContent = new_word;
            }, false );
}( document ));



Yes, I figure they must work but the text to be replaced is put there dynamically, that might be preventing it from working.

Not sure. Just know they they didn’t work on my page.

Have you not considered showing the members here your problem page ?

Or are you now no longer interested in finding a solution?



All of the code examples are designed to do the replacement.
They are not designed to retrieve the text from your page, or to update your page with the updated text.

Help cannot be given with updating your page until you give us information about your page.


This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.