Find and Replace special char with defined value - javascript

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

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

How to split a number after n digits in javascript?

I have a number 2802 which is stored as a string in the backend. I want to split this number into 2 parts as 28 and 02 which should be shown as date 28/02? How can I do it with '/' in between them?
Try this : you can use substring as shown below. substring used for getting string between start index and end index. In your case, get string from 0 to 1 and then 2 to 3.
var str = "2802";
str = str.substring(0,2) + "/" + str.substring(2,4);
alert(str);
More information on Substring
Solution with regex
var res = "2802".match(/\d{2}/g).join('/');
document.write(res);
Are you asking about simple string manipulation?
var str = "1234";
var res = str.substr(0, 2)+"/"+str.substr(2,4);
You can do this:
var str = "2802";
str = str.split('').map(function(el, i){
if(i == 2){ el = '/'+el}
return el;
});
document.querySelector('pre').innerHTML = str.join('');
<pre></pre>
With regular expression:
var str = "2802";
str = str.replace(/(.{1,2}$)/gi, '/$1');
document.querySelector('pre').innerHTML = str;
<pre></pre>
var str = "2802";
var output = [str.slice(0, 2), str.slice(2)].join('/');
In this context conside 2802, 28 is date and 02 is month
Here 112, 028 what is date and month ?
A more generic solution could be
var num = 2802;
var output = String(num).match(new RegExp('.{1,2}', 'g')).join("/");
replace 2 with which ever number to split the number after n digits.
var n = 2;
var output = String(num).match(new RegExp('.{1,'+n+'}', 'g'));
var db_date = '112';
var date_str = '';
if(db_date.slice(0,1)==0){
var date_val = db_date.slice(0,2);
var month_val = db_date.slice(2,3);
if(month_val<=9){
month_val = '0'+month_val;
}
}else{
var date_val = db_date.slice(0,1);
date_val = parseInt(date_val);
if(date_val<=9){
date_val = date_val.toString();
date_val = '0'+date_val;
}
var month_val = db_date.slice(1,3);
}
alert(date_val+'/'+month_val);

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, '\\)')

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