replacing last word of string using node js / javascript - javascript

var str = "Demo Docs Version 1.0.1";
var got = str.split(" ").splice(-1)[0]
str = str.replace(got, "testing");
console.log(str);
I can replace last word if there is space in between but how can i replace my last word when there _ in string ?
var str = "Demo-Docs-Version-1-0-1";
var got = str.split(" ").splice(-1)[0]
str = str.replace(got, "testing");
console.log(str);

What about this?
str = str.replace(/(?!.*(\s|_)).*/, "testing")

Related

How to use preg_replace in angualr? [duplicate]

var str = 'asd-0.testing';
var regex = /asd-(\d)\.\w+/;
str.replace(regex, 1);
That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);
Or if you're sure there won't be any other digits in the string:
var str = 'asd-0.testing';
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
using str.replace(regex, $1);:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
if (str.match(regex)) {
str = str.replace(regex, "$1" + "1" + "$2");
}
Edit: adaptation regarding the comment
I would get the part before and after what you want to replace and put them either side.
Like:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
var matches = str.match(regex);
var result = matches[1] + "1" + matches[2];
// With ES6:
var result = `${matches[1]}1${matches[2]}`;
I think the simplest way to achieve your goal is this:
var str = 'asd-0.testing';
var regex = /(asd-)(\d)(\.\w+)/;
var anyNumber = 1;
var res = str.replace(regex, `$1${anyNumber}$3`);

How to capture variable string in replace()?

Code like this:
var v = 'd';
var re = new RegExp('a(.*?)' + v, 'gi');
"abcd".replace(re,re.$1);
I want to get "bc".
Use simply $1 in a string to get the result of the first capturing group:
var re = /a(.*)d/gi
var output = "abcd".replace(re,"$1")
console.log(output) //"bc"
You can do this easily with:
let str = "abcd";
let bc = str.replace(/a(.*)d/g,"$1");
console.log(bc) //bc
The "$1" captures whatever is in the regex () bracket.

Find and Replace special char with defined value

I want to find and replace if a string contain /,+,?,-,_,# any of these into _sls_,_pls_,_wht_,_dsh_,_usc_,_hsh_ this respectively.
Example:
'_sls_'=>'/','_pls_'=>'+','_wht_'=>'?','_dsh_'=>'-','_usc_'=>'_','_hsh_'=>'#'
Mary_land = Mary_usc_land
Mary+land = Mary_pls_land
Write all the characters you want to get replaced into a function and call it
var string1 = "Mary_land";
var string2 = "Mary+lang";
var string3 = "Mary#lang";
var string4 = "Mary-lang";
var string5 = "Mary/lang?Maryland";
console.log(normalize(string1));
console.log(normalize(string2));
console.log(normalize(string3));
console.log(normalize(string4));
console.log(normalize(string5));
function normalize(str){
str = str.replace(/_/g,"_usc_");
str = str.replace(/\+/g,"_pls_");
str = str.replace(/\//g,"_sls_");
str = str.replace(/#/g,"_hsh_");
str = str.replace(/-/g,"_dsh_");
str = str.replace(/\?/g,"_wht_");
return str;
}

how to remove white space at the beginning and end of a regular expression?

I create a javascript parser for my markup language.
I wish no spaces between tags and content like this:
__underline a sentence __and more.
(or correctly : __underline a sentence__ and more.)
to :
<u>underline a sentence</u> and more.
But result is in the first case :
<u>underline a sentence </u>and more.
My code :
var tabML = ['\'','*','_'],
tabHTML = ['em','strong','u']
tag, char;
for(var i=0; i<tabML.length; i++){
tag = tabHTML[i]; char = tabML[i];
regex = new RegExp(char+char+'(.+)'+char+char, 'ig');
txt = txt.replace(regex, '<'+tag+'>$1</'+tag+'>');
}
Thanks.
Use the below regex and then replace the matched characters with <u>$1</u>
__\s*(.*?)\s*__\s*
DEMO
> var s1 = "__underline a sentence __and more."
undefined
> var s2 = "__underline a sentence__ and more."
undefined
> s1.replace(/__\s*(.*?)\s*__\s*/g, '<u>$1</u> ')
'<u>underline a sentence</u> and more.'
> s2.replace(/__\s*(.*?)\s*__\s*/g, '<u>$1</u> ')
'<u>underline a sentence</u> and more.'
__(.*?)[ ]*__[ ]*
Try this.Replace by <u>$1</u>.See demo.
https://regex101.com/r/tX2bH4/5
var re = /__(.*?)[ ]*__[ ]*/gm;
var str = '__underline a sentence __and more.\n__underline a sentence__ and more.';
var subst = '<u>$1</u> ';
var result = str.replace(re, subst);

How can I replace a regex substring match in Javascript?

var str = 'asd-0.testing';
var regex = /asd-(\d)\.\w+/;
str.replace(regex, 1);
That replaces the entire string str with 1. I want it to replace the matched substring instead of the whole string. Is this possible in Javascript?
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
str = str.replace(regex, "$11$2");
console.log(str);
Or if you're sure there won't be any other digits in the string:
var str = 'asd-0.testing';
var regex = /\d/;
str = str.replace(regex, "1");
console.log(str);
using str.replace(regex, $1);:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
if (str.match(regex)) {
str = str.replace(regex, "$1" + "1" + "$2");
}
Edit: adaptation regarding the comment
I would get the part before and after what you want to replace and put them either side.
Like:
var str = 'asd-0.testing';
var regex = /(asd-)\d(\.\w+)/;
var matches = str.match(regex);
var result = matches[1] + "1" + matches[2];
// With ES6:
var result = `${matches[1]}1${matches[2]}`;
I think the simplest way to achieve your goal is this:
var str = 'asd-0.testing';
var regex = /(asd-)(\d)(\.\w+)/;
var anyNumber = 1;
var res = str.replace(regex, `$1${anyNumber}$3`);

Categories

Resources