replace number from the middle using javascript / node js - javascript

var str = "demo-test-1-0-4_testing.pdf";
I have a string i want to replace with word verified
so my expected value look like this
str= demo-test-verified_testing.pdf
I can do this if the numbers are written in last like this
var str = "demo-test-1-0-1";
str = str.replace(/(?!(-| |_))(\d|-|\.)+\s*$/, "verified");
console.log(str);
but in middle I don't know
as my expected value is like this demo-test-verified_testing.pdf as i want to replace 1-0-1 i can use replace function but every time version get updated so number get changed that's why i cant use replace function

This should be enough:
var str = "demo-test-1-0-4_testing.pdf";
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)
str = "demo-test-1-0-1"
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)
var str = "demo-test-1-0-41_testing.pdf";
str = str.replace(/(-\d+)+/,"-verified")
console.log(str)

Related

How to regex, and add "-" between words?

UPDATED
I been looking around in the old interweb to see if there is any way I can regex this as part of a replace method I'm doing: str.replace(/\w[A-Z]/gm, "-")
thisIsARegex
into this:
this-Is-A-Regex
I tried to mess around on regex101 with matching a \w character followed by [A-Z] but failed.
Any thoughts?
If the first char can't be uppercase:
var str = "thisIsARegex";
str = str.replace(/(?=[A-Z])/g, "-");
console.log(str); // this-Is-A-Regex
If the first char can be uppercase:
var str = "ThisIsARegex";
str = str.replace(/.(?=[A-Z])/g, "$&-");
console.log(str); // This-Is-A-Regex
or
var str = "ThisIsARegex";
str = str.replace(/\B(?=[A-Z])/g, "-");
console.log(str); // This-Is-A-Regex
(Last snippet suggested by #Thomas.)
var s = "thisIsARegex";
s = s.replace(/([A-Z])/g, '-$1').trim();
console.log(s);
Try this one:
you can check regex on this page and make your own tests:
https://regexr.com/
// initial value
let text = "thisIsARegexText";
// select Uppercase characters
let regexPattern = /[^a-z]/g;
// dump temp array
let newText = [];
// go through all characters, find Uppercase and replace with "-UppercaseCharacter"
for(i of text){
newText.push(i.replace(/[^a-z]/g, "-" + i))
}
// assign the result to the initial variable
text = newText.join("");

Extract words with RegEx

I am new with RegEx, but it would be very useful to use it for my project. What I want to do in Javascript is this :
I have this kind of string "/this/is/an/example" and I would like to extract each word of that string, that is to say :
"/this/is/an/example" -> this, is, an, example. And then use each word.
Up to now, I did :
var str = "/this/is/a/test";
var patt1 = /\/*/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
and it returns me : /,,,,,/,,,/,,/,,,,,
I know that I will have to use .slice function next if I can identify the position of each "/" by using search for instance but using search it only returns me the index of the first "/" that is to say in this case 0.
I cannot find out.
Any Idea ?
Thanks in advance !
Use split()
The split() method splits a String object into an array of strings by separating the string into substrings, using a specified separator string to determine where to make each split.
var str = "/this/is/a/test";
var array = str.split('/');
console.log(array);
In case you want to do with regex.
var str = "/this/is/a/test";
var patt1 = /(\w+)/g;
var result = str.match(patt1)
console.log(result);
Well I guess it depends on your definition of 'word', there is a 'word character' match which might be what you want:
var patt1 = /(\w+)/g;
Here is a working example of the regex
Full JS example:
var str = "/this/is/a/test";
var patt1 = /(\w+)/g;
var match = str.match(patt1);
var output = match.join(", ");
console.log(output);
You can use this regex: /\b[^\d\W]+\b/g, to have a specific word just access the index in the array. e.g result[0] == this
var str = "/this/is/a/test";
var patt1 = /\b[^\d\W]+\b/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
<span id="demo"></span>

javascript regex exec and replace

I want to replace the regex matched str with some other string after calling function.
Can't find any method for the same ..
Consider
var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;
while ((m = re.exec(str)) != null) {
// m= ["someRegex", "e"]
// m= ["someRegeeeeex", "eeeee"]
// I WANT TO REPLACE THE MATCHED WITH AFTER CALLING A FUNCTION
// lets say someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5
}
Note : i don't want to use str replace since it will not be give me the correct solution. lets say replacing someRegex with someRegex1 and someRegeeeeex with someRegeeeeex5 . USING str.replace will replace first one again and again.
It looks like you're trying to append the length of the group's value to the match. If that's the case, replace can be used with a function:
var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;
var result = str.replace(re, function(match, group) {
return match + group.length;
});
console.log(result);
Refer to Specifying a function as a parameter for the String.prototype.replace method.
First, use a while to loop on your string.
Then inside the loop use replace to update the founded string.
var str = 'one two [%test1%] try some test [%test2%]';
var regex = /\[%(.*?)\%]/g;
function populateStudioMacro() {
var result;
while((result = regex.exec(str)) !== null) {
// match found
document.body.innerHTML += 'Found<br>';
document.body.innerHTML += result[0] + '<br>';
document.body.innerHTML += result[1] + '<br>';
// replace string matched
str = str.replace(result[0], ' =>new string<= ');
}
document.body.innerHTML += str;
}
populateStudioMacro();
USING str.replace will replace first one again and again.
This is only true if you pass the /g modifier. Not sure what your actual data is so that may or may not be relevant.
Again without knowing your actual data, I think I'd use match() here instead of exec() and reconstruct the string from matches. E.g.
var str = "someRegex ajfgdgkjdljlgdgjlrm someRegeeeeex dfdsfj";
var re = /someReg(.*?)x/g;
str.match(re);
produces
["someRegex", "someRegeeeeex"]

How to use Javascript slice to extract first and last letter of a string?

How to use JavaScript slice to extract the first and last letter of a string?
Eg: "Hello World"
I need the result as "dH".
Following is my jsfiddle :
http://jsfiddle.net/vSAs8/
Here's the cleanest solution :
var output = input.slice(-1)+input[0];
If you want more slice, there's also
var output = input.slice(-1)+input.slice(0,1);
And here are alternate fun (and less efficient) solutions :
var output = input.replace(/^(.).*(.)$/,'$2$1');
or
var output = input.match(/^.|.$/g).reverse().join('');
Substr works as well:
alert(test.substr(-1,1) + test.substr(0,1));
a.charAt(a.length-1) + a.charAt(0)
var str = " Virat Kohali "
var get_string_label = function(str){
str = str.split(" ");
str = str.filter(res=>res.length>0);
str = str.map(function(res){
return res[0].toUpperCase();
});
str = str.join("");
return str;
};
console.log(get_string_label(str));
str.split(" "); method splits a String object into an array of strings by separating the string into substrings, where it will find space in string.
then str.filter(res=>res.length>0) will filter out string having zero length (for "virat kohali" string you will get empty sub-string)
after that using map function you can fetch your first letter

Replace comma by double quote in javascript

How to replace comma by double quote in javascript?
For example: "c,C#,JavaScript" should be like "C","C#","JavaScript"
str = '"c,C#,JavaScript"';
str = str.split(',').join('","');
That would result in "c","C#","JavaScript"
var original = '"c,C#,JavaScript"';
var quoted = original.replace(/,/g, '","'); // "c","C#","JavaScript"
Just to toss it in there, you could also .split() and .join(), like this:
var oldString = '"c,C#,JavaScript"';
var newString = oldString.split(',').join('","');
You can test it here.
You can do this:
str = "c,C#,JavaScript";
str = str.replace(/,/g, '"');
Result:
c"C#"JavaScript

Categories

Resources