Hi I’m trying to replace:
Economy1
with:
Economy
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
Hi I’m trying to replace:
Economy1
with:
Economy
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
<script>
var string = 'Economy1';
var newstring = string.replace(/1/, '');
document.write(newstring);
</script>
Hi there Freejoy,
this…
<script>
(function( d ) {
'use strict';
var word = 'Economy1', new_word;
new_word= word.replace( word.charAt( word.length - 1 ), '' );
console.log( new_word );
}( document ));
</script>
…will replace any character at the end of the string.
coothead
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.
coothead
Or if you just want all but the last character, you can use the slice method.
var result = "abc9".slice(null, -1) // "abc"
Hi there Freejoy,
here is a working example- using @Paul_Wilkins’ short method…
<!DOCTYPE HTML>
<html lang="en">
<head>
<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;
}
</style>
</head>
<body>
<h1>Economy1</h1>
<button>amend</button>
<script>
(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 ));
</script>
</body>
</html>
coothead
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?
coothead
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.