I have a string which has email id as plain text in it ,I want to replace the email id in the string with hashed value eg.
var str ="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
Before storing the above string I want to hash only testmail5#gmail.com which is after userId=. Hence need suggestions to achieve the same.
Using split twice
var str="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
console.log(str.split('userId=')[1].split('|')[0])
Try this :
str.match(/\userId=(.*?)\|/)[1]
Regex are better to do stuff like that
Quick and dirty way by using URL API in JS.
var data = "Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB";
var some_hash = "myHashValue";
var data_url = new URL("http://localhost/?"+data); // dirty part
var new_data = data.replace(data_url.searchParams.get("userId").split('|')[0], some_hash);
console.log(new_data);
Related
For example I have a string getting from current URL using javascript
hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc
How do I replace character in the end OrderDateAsc ? If I want to replace for example , OrderDateDesc , how should I do it using Javascript ? The URL infront might be differs all the time , they keyword should be &sortBy.
Please help ,thanks.
var currentUrl = window.location.href;
var url = new URL(currentUrl);
var c = url.searchParams.get("sortBy");
You can get sortBy value like this, and maybe you can use some if statements for if set or not , and you can set like this:
url.searchParams.set('sortBy',"WhateverYouWant");
Also,convert url to string, you can read parameters url.searchParams.get("sortBy"); and again some if statements,
if change :
url = url.Replace(sortByValue,"WhateverYouWant");
this will works too.
But if your string looks like that and you try to change search value, then you will change every "search" value and its not work. :
hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc
change search as WhateverYouWant
hostname/report/WhateverYouWantDate?WhateverYouWantOrderID=&WhateverYouWantDateFrom=2018-10-16&WhateverYouWantDateTo=2018-10-23&WhateverYouWant=WhateverYouWant&sortBy=OrderDateAsc
You see, its bad :)
You could do it with the following code.
//your string
let str = "hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc";
//find "OrderDateAsc" and replace it with "OrderDateDesc"
str = str.replace("OrderDateAsc", "OrderDateDesc" );
you can use regular expression and replace the sortBy param value like that:
const url ='hostname/report/searchDate?searchOrderID=&searchDateFrom=2018-10-16&searchDateTo=2018-10-23&search=search&sortBy=OrderDateAsc';
const regex = /(&sortBy=OrderDate)(Asc)/gi;
const newUrl = url.replace(regex, '$1Desc');
I am familiar with the "replace" method in javascript is not enough. Possibly is it for my project is also a better way, then I would be glad if you also tells me this.
I would need the following:
I have a string with the following content:
var address = 'Beethovenstrasse 1, 97080 Würzburg';
This should be converted to:
var addressNew = '97080/W%C3%9CRZBURG/Beethovenstrasse/1';
And another string
var address = 'Hitzelsbergstr. 84, 83233 Bernau';
This should be converted to:
var addressNew = '83233%20Bernau,%20Hitzelsbergstr.%2084';
To explain why I need this: I need to convert the addresses are in the form of the string "address" in a database. The new string "addressNew" are then part of a link, which starts on mobile devices or TOMTOM navigation NAVIGON and the address to the destination passes by.
Ever Thanks for your help
A simple solution:
var address = 'Beethovenstrasse 1, 97080 Würzburg';
var parts = address.split(', ');
var newString = parts[1] + '/' + parts[0];
console.log(encodeURIComponent(newString));
This solution is very specific and does not handle other formats, but you should get some ideas.
You can split the string based on spaces and than you can adjeust the order and repalce characters with %20
I need to strip out the IDs of embedded youtube videos, so I have the url which is something like:
www.youtube.com/embed/[someID]&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1
All I want is the [someID] string. I have declared an empty array to store the regex matches;
var videoID = [];
The closest I have come to a solution is:
videoID = videoID.match("embed/(\w*)");
but this results in the following:
video[0] ("embed/")
video[1] ()
Use this:
url = "www.youtube.com/embed/someID&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1";
Then use either:
var videoID = url.match(/embed\/(\w*)/); // regex
OR else:
var videoID = url.match("embed/\\w*)"); // regex object
Both will give this output:
["embed/someID", "someID"]
If you provide a string then String#match method will attempt to construct a RegExp object and for that case you need to use \\w instead of \w.
Try this you may get id. I didn't use regex but still we can get id from above demo like urls.
var url ="www.youtube.com/embed/[someID]&rel=0&controls=0&showinfo=0&frameborder=1&modestbranding=1";
var urlQueries = url.split('/');
var queryParameters = urlQueries[2].split('&'); // arrays of all query parameters
var id = queryParameters[0]; // get ids at index 0
Working demo here
I'm not sure if jquery can do Positive-Lookbehinds, but try this (?<=embed\/)([^&]*)
If not, then (?:embed\/)([^&]*) is closer to your example.
Demo:
http://regex101.com/r/nW5uL8
How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"?
var name = "nameGorge"; //output Gorge
No jQuery needed! Just use the substring method:
var gorge = name.substring(4);
Or if the text you want to remove isn't static:
var name = 'nameGorge';
var toRemove = 'name';
var gorge = name.replace(toRemove,'');
Using .split(). (Second version uses .slice() and .join() on the Array.)
var result = name.split('name')[1];
var result = name.split('name').slice( 1 ).join(''); // May be a little safer
Using .replace().
var result = name.replace('name','');
Using .slice() on a String.
var result = name.slice( 4 );
Standard javascript will do that using the following syntax:
string.substring(from, to)
var name = "nameGorge";
var output = name.substring(4);
Read more here: http://www.w3schools.com/jsref/jsref_substring.asp
That's just plain JavaScript: see substring and substr.
You don't need jquery in order to do that.
var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);
var name = "nameGorge";
name.match(/[A-Z].*/)[0]
Yes you can, although it relies on Javascript's inherent functionality and not the jQuery library.
http://www.w3schools.com/jsref/jsref_substr.asp
The substr function will allow you to extract certain parts of the string.
Now, if you're looking for a specific string or character to use to find what part of the string to extract, you can make use of the indexOf function as well.
http://www.w3schools.com/jsref/jsref_IndexOf.asp
The question is somewhat vague though; even just link text with 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?
How about the following?
<script charset='utf-8' type='text/javascript'>
jQuery(function($) { var a=$; a.noConflict();
//assumming that you are using an input text
// element with the text "nameGorge"
var itext_target = a("input[type='text']:contains('nameGorge')");
//gives the second part of the split which is 'Gorge'
itext_target.html().split("nameGorge")[1];
...
});
</script>
$('img').click(function(){
var add_to_list = $(this);
// database query for element
});
Currently add_to_list is getting the value 'images/image.jpg'. I want to replace the 'image/' to nothing so I only get the name of the picture (with or without the extension). How can I do this? I couldn't find anything. Also please provide me with further reading on string manipulation please.
Have you tried javascript replace function ?
You should modify your code to something like this:
$('img').click(function(){
var add_to_list = $(this).attr('src').replace('images/', '');
// database query for element
});
use
add_to_list.substring(7);
This will get you the string after the 7th character. If there might be longer paths, you can split() into an array and get the last part of the path using pop().
add_to_list.split("/").pop();
substring
split
pop
This tutorial explains many of the string manipulation methods seen in the answers here.
$('img').click(function(){
var add_to_list = $(this).attr('src').replace('image/', '');
// database query for element
});
var pieces = add_to_list.split('/');
var filename = pieces[pieces.length-1];