How can I replace a regex substring match in Javascript? - 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`);

Related

replacing last word of string using node js / 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")

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`);

Split string on two delimiters?

I want to split a string based on the delimiters " " and "," (including quotation marks). How would I be able to do this? I tried doing this:
var str = '"String","String2" "String3"';
str = str.split('" "', '","');
console.log(str);
But it didn't work. I was expecting this console output:
["String", "String2", "String3"]
But I got:
[]
How do I split a string based on two delimiters? Is it possible?
let str = '"String","String2" "String3"';
str = str.split(/ |,/);
console.log(str);
let str2 = '"String","String2" "String3"';
str2 = str2.split(/" "|","|"|\n/).filter(Boolean);
console.log(str2);
let str3 = '"String","String2" "String3"';
str3 = str3.match(/[\w\d]+/g);
console.log(str3);
You can specify regex or array as delimiter
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split
try below solution
var str = '"String","String2" "String3"';
str = str.match(/\w+|"[^"]+"/g)
str = str.map(function(x){return x.replace(/"/g, '');});
console.log('Final str : ', str);
You can use regex: str = str.split(/,| /)
console.log("a,b c".split(/,| /));

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;
}

Javascript regex help

I have the following string in JavaScript
var mystring = " abcdef(p,q); check(x,y); cef(m,n);"
I would want to do a string replace such that my final string is :
mystring = " abcdef(p,q); someothercheck\(x,y\); cef(m,n);"
x and y should remain same after the substitution. and the backslashes are necessary since I need to pass them to some other command.
There can be other Parantheses in the string too.
If you don't have other parenthesis, it should be easy.
mystring = mystring.replace("check(", "someothercheck\\(");
mystring = mystring.replace(")", "\\)");
EDIT This works also in the case of multiple parenthesis (It does not affect the empty ones).
var str=" abcdef; check(x,y); cef();"
patt = /((\w)/g;
// transform (x in \(x
str = str.replace(patt, '\\($1');
patt = /(\w)\)/g
// transform y) in y\);
str = str.replace(patt, '$1\\)');
// transform check in someothercheck
str = str.replace('check', 'someothercheck');
EDIT Now it converts only the check strings.
function convertCheck(str, check, someOtherCheck) {
// console.log(str + " contains " + check + "? ");
// console.log(str.indexOf(check));
if (str.indexOf(check) === -1) return str;
var patt1 = /\((\w)/g,
patt2 = /(\w)\)/g;
str = str.replace(patt1, '\\($1');
str = str.replace(patt2, '$1\\)');
str = str.replace(check, someOtherCheck);
return str;
}
var str = "abcdef(); check(x,y); cef();",
tokens = str.split(';');
for (var i = 0; i < tokens.length; i++) {
tokens[i] = convertCheck(tokens[i], "check", "someothercheck");
}
str = tokens.join(";");
alert(str); // "abcdef(); someothercheck/(x,y/); cef();"
var myString = "abcdef; check(x,y); cef;";
myString.replace(/(\w+)\(/, 'someother$1(')
.replace(/\(/g, '\\(')
.replace(/\)/g, '\\)')

Categories

Resources