Regular expression for replacing string with javascript - javascript

I need help in writing regular expression:
part of my string is fixed and another part of its variable.
only if fixed AND variable string exist i need to alter the string other wise no.
Fixed string:example: AA.BBB.COM
Variable string (may or mayn't exist ): US, but if exist it will be always two letter string with any combination of letter.
In below string if I have variable two letter string exist I want to append “.new”
1 ) https://XY**.US**.AA.BBB.COM
Output: https:// XYZ12**.US.NEW**.AA.BBB.COM
2 ) https://XY.UK.AA.BBB.COM
Output: https:// XYZ12.UK.NEW.AA.BBB.COM
3) https://XY.AA.BBB.COM (no variable string so no change)
Output: https:// XY.AA.BBB.COM
Thanks for your help .
Raghav

Something like the following should get you started, there are other methods. Splitting and parsing might suit better depending on your real requirements:
var s = 'https://XY.US.AA.BBB.COM';
var t = 'https://XY.UK.AA.BBB.COM';
var u = 'https://XY.AA.BBB.COM';
var re = /(\.)(UK|US)(\.)/;
alert(
s.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
t.replace(re, '$1' + '$2' + '.NEW' + '$3') + '\n' +
u.replace(re, '$1' + '$2' + '.NEW' + '$3')
);

Related

indexOf for multiple options

Let say, I get the following using var content = this.innerHTML:
w here </div>
Using indexOf (or other ways), I want to check for the first position that has either "Space", "<" or "&nbsp".
In this case, it will be 1 (after "w").
What I am confused about is how do I check for the very first position that has either one of these three choices? Do I use Do...while to check for individual "options"?
You're probably looking for a Regular Expression (Regex) and the String#search method. Regex is a bit much to learn all at once, but I'll explain this example code.
You can use square brackets to denote a set of characters, so for example [ <] says "match either a space or a less-than sign."
You can use the pipe | to separate possibilities if you want to match one pattern or another, and that's how to account for matching a non-breaking space HTML entity.
var string = 'w here </div>',
index = string.search(/[ <]| /)
console.log(index) //=> 1
You can use a regular expression with alternations (|), which means "match one of these things". That will also tell you what you found, if that's useful:
function check(str) {
var m = / |<| /.exec(str);
if (!m) {
console.log("Not found in '" + str + "'");
return;
}
console.log("'" + m[0] + "' found at index " + m.index + " in '" + str + "'");
}
check("w here </div>");
check("where </div>");
check("where</div>");

buillding regex expression

i want to replace specific amount of white-spaces from a String at the beginning and i can use
replace(/^\s{2}/g,"");
and it works .but 2 should be changed according to a value of a variable .so i need to construct a new RegExp()
so i used
var lead=2;
var regex = new RegExp("\^\\s{" + lead + "}//g");
alert("regex "+regex);
real output
/^\s{2}\/\/g/
expected output
/^\s{2}/g
could you help me to fix this problem.tnx
As the param to RegExp is the regex, you don't need the / delimiters. Use the flags as the second parameter to the RegEx() constructor.
var regex = new RegExp("^\\s{" + lead + "}", 'g');
Example:
var lead = 2;
var regex = new RegExp("^\\s{" + lead + "}", 'gmi');
alert(regex);
var str = ' Say My Name';
alert(str.replace(regex, ''));
new RegExp("^\\s{" + lead + "}", "g");
Try
"\^\\s{" + lead + "}\/g"
instead of
"\^\\s{" + lead + "}//g"
I think you wanted to escape the "/", but you accidently used "/" instead of "\".

How can I change the text in just a part of my string with javascript?

I have a javascript string that contains the following:
data-href="/Admin/Edit?pk=0001I&rk=50050055"
How can I change this string so the value of rk is changed to a value held in the string newRowKey? Not sure if it helps but the data format always looks like this with the rk followed by an = and then terminated with the "
datahref="/Admin/Edit?pk=0001I&rk=50050055";
to change it if your building the URL
datahref="/Admin/Edit?pk=0001I&rk="+ newRowKey;
or replace it if you know the existing value
datahref.replace("50050055", newRowKey);
if you do not know the value of rk, but you know its last in the URL, you could use indexOf to find it.
datahref = datahref.substring(0,datahref.indexOf("rk=")+3) + newRowKey;
Maybe you want a regular expression to replace that rk.
newRowKey = 'XXXXXX'
s = 'Edit?pk=0001I&rk=50050055';
s2 = s.replace(/(.*;)(rk=)(.*)(&|)/, '$1$2' + newRowKey)
alert( s + "\n" + s2 );
//2nd test
s = 'Edit?pk=0001I&rk=50050055&';
s2 = s.replace(/(.*;)(rk=)(.*)(&|)/, '$1$2' + newRowKey)
alert( s + "\n" + s2 );
​demo: http://jsfiddle.net/fedmich/g3TjE/
data_href.replace(/rk=.*$/,'rk=' + newRowKey);

Removing %20 value from get method

Removing %20 in get method?
var c=new Array(a);
(eg: a={"1","2"}) window.location="my_details.html?"+ c + "_";
and in my_details.html :
var q=window.location.search;
alert("qqqqqqqqqqqqq " + q);
var arrayList = (q)? q.substring(1).split("_"):[];
var list=new Array(arrayList);
alert("dataaaaaaaaaaaa " + list + "llll " );
and in "list" its dusplaying me "1%202";
How can I remove this %20 =space value ??
Thanks
just use this:
alert("dataaaaaaaaaaaa " + decodeURIComponent(list) + "llll " );
This should decode the %20 to space
look here: http://www.w3schools.com/jsref/jsref_decodeURIComponent.asp
If there is a space in the parameter(s), then the %20 (URL Encoding) is necessary. You cannot pass a space in a GET request.
If you need to avoid this, use POST.
As far as I can see the problem is being introduced at this line:
window.location="my_details.html?"+ c + "_";
This could be written as:
window.location="my_details.html?"+ c.toString() + "_";
The default .toString() of a JavaScript Array would be to use a delimiter of ,, i.e.
var str = ["1", "2", "3"].toString(); // 1,2,3
In you example it appears that the delimiter being used is a space. This would have been changed by something changing the default behaviour of .toString() on the Array.prototype. Try using the following:
window.location="my_details.html?"+ c.join(",") + "_";
Better to use replace() method to replace %20 to space
list.replace("%20"," ");

JavaScript regex: find non-numeric character

Let's say I have these two strings: "5/15/1983" and "1983.05.15". Assume that all characters in the string will be numeric, except a "separator" character that can appear anywhere in the string. There will be only one separator character; all instances of any given non-numeric character in the string will be identical.
How can I use regex to extract this character? Is there a more efficient way than the one below?
"05-15-1983".replace(/\d/g, "")[0];
Thanks!
"05-15-1983".match(/\D/)
Technically, this returns an array containing one string, but it will implicitly convert to the string most places you need this.
Though i could not exactly get what you trying to do i tried to extract the numbers only in one string and the seperator in next string.
I used the above:
<script>
var myStr1 = "1981-01-05";
var myStr2 = "1981-01-05";
var RegEx1 = /[0-9]/g;
var RegEx2 = /[^0-9]/g;
var RegEx3 = /[^0-9]/;
document.write( 'First : ' + myStr1.match( RegEx1 ) + '<br />' );
document.write( 'tooo : ' + myStr2.replace( RegEx2, "" ) + '<br />' );
document.write( 'Second : ' + myStr1.match( RegEx2 ) + '<br />' );
document.write( 'Third : ' + myStr1.match( RegEx3 ) + '<br />' );
</script>
Output:
First : 1,9,8,1,0,1,0,5
tooo : 19810105
Second : -,-
Third : -
I hope you get your answer
Clearly tired or not paying attention on my previous answer. Sorry about that. What I should have written was:
var regexp = new RegExp("([^0-9])","g");
var separator = regexp.exec("1985-10-20")[1];
Of course, Matthew Flaschen's works just as well. I just wanted to correct mine.

Categories

Resources