Javascript name field masking (Using regular expression) - javascript

i need to mask a name and i want it in Script.
i have a list format data.
Following is my jsp which put data in a variable.
<c:forEach value="${test}" var="v"/>
<c:set var = "nametest" value="${v.name}"
i only need names to be shown on the screen. so i made a variable only contains names. The problem is my script function doesn't look like
contain my data correctly.
Following is my script.
function maskingName(nametest){
var a = "${nametest}"
if (a === undefined || a ===''){
return '';
}
var pattern = /.$/;
return a.replace(pattern,"*");
}
after running it, i only get the last name (there are 10 names and it shows the only the last one)without masking.
My question is
1. how can i use List format in my script function?
2. why does the regular expression not working?
Thank you!

I have written a small java code that might help you understand better
static String maskingName(String nametest) {
if (nametest == null || nametest == "") {
return "";
}
String pStr = ".$" // ".*$";
Pattern pattern = Pattern.compile(pStr);
return pattern.matcher(nametest).replaceAll("***");
}
For input '12345' or '345345', the output will be '1234***' or '34534***'
Replacing pStr=".*$" will give output ******

Related

How do I create a custom javascript variable that selects part of an already existing javascript variable?

I am trying to create a custom javascript variable in GTM that returns part of a javascript variable that already exists.
Variable that already exists: window.ShopifyAnalytics.meta.product.variants.0.name
returns this: "Bamboo Basic String - Schwarz - S"
However I want to code a custom javascript variable to just return the Schwarz part, is this possible? If so what is the code that I would need?
Please can someone let me know what code to put into GTM to create this variable?
TIA
If all names are pretty much the same you could use split to get that part of string and then remove whitespaces. It would look like this:
window.ShopifyAnalytics.meta.product.variants.0.name.split('-')[1].replace(/
/g,'');
If the already existing variable is always structured the same way you could do something like this:
let variable = window.ShopifyAnalytics.meta.product.variants.0.name.split('-')
Then by calling varaible[1] you get the 'Schwartz' part of the variable.
If you want a return value you can use a function like the following and call it wherever you want.
Simply make sure to pass the correct argument content
// Declaring a function getColor that returns the second element in the list,
// trimmed (without spaces before and after)
const getColor = (content) => {
return content.split('-')[1].trim();
}
const test = "Bamboo Basic String - Schwarz - S";
console.log(getColor(test));
//console.log(getColor(window.ShopifyAnalytics.meta.product.variants.0.name));
You could split the string on the hypens (-) like this:
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
const part = productName.split(' - ')[1];
Assuming you have a consistent format, and you always want the second part after that hyphen.
split will separate parts of a string into an array where it finds a match for the argument. The first index [0] will be the product name, the second [1] will be the part you're looking for.
This could cause issues if you have a product name with a - in it too though so use with care!
If it needs to be an anonymous function for GTM, you could try the following (though I'm not a GTM expert):
function () {
const productName = window.ShopifyAnalytics.meta.product.variants.0.name;
return productName.split(' - ')[1] || 'Unknown';
}

How to split data with square brackets while declaring parameter in json

I am passing some data from js file to handler which in turn gets result from SP. I have a parameter like ID = abc[123]. but i have to pass only 123 as value to ID to SP.
This is how im declaring parameter in js
var parameters = JSON.stringify({
"ID": JSON.stringify(EditedID).replace(/]|[[]/g, '')
});
But i am getting error like invalid ID
Kindly help
Currently, you are replacing the regex with empty space, so it will return the result 'abc123'. What you actually need is getting the string inside the brackets. You can use the below code to do it.
var EditedID = "abc[123]"
var regex = /\[([^\[\]]*)\]/
var result = ""
match = JSON.stringify(EditedID).match(regex)
if (match != null) {
result = match[1]
}
"result = match[1]" means that it will assign the value inside the brackets to result. If you want the value with both brackets, use match[0].
I assume that your EditedID is an object and somehow it will need the method "JSON.stringify" to make it become String. If your EditedID is already a String, just replace the match value to make it simpler
match = EditedID.match(regex)
If there is not match, the code won't run into the if condition so the value of result will just be an empty String.

Jquery Ajax multiple tag search

Hi everyone i have one problem with my ajax tag search system. Following code working fine just one searching tag. But if i search multiple tag then it is not working. What i need to do here? What i am missing anyone can help me here ?
For example: When i write #innovation the the ajax will showing #innovation but when i search multiple tag like #innovation asking #stackoverflow then it is not searching #stackoverflow
$(document).ready(function() {
var start=/#/ig;
var word=/#(\w+)/ig;
$("#contentbox").live("keyup",function() {
var content=$(this).text();
var go= content.match(start);
var name= content.match(word);
var dataString = 'searchword='+ name;
if(go!==null && go.length!==0) {
$("#msgbox").slideDown('show');
$("#display").slideUp('show');
$("#msgbox").html("Type the name of someone or something...");
if(name!==null && name.length!==0) {
$.ajax({
type: "POST",
url: "boxsearch.php",
data: dataString,
cache: false,
success: function(html) {
$("#msgbox").hide();
$("#display").html(html).show();
}
});
}
}
return false;
});
});
I think the problem is the following line :
var start=/#/ig;
var word=/#(\w+)/ig;
There's a million different ways to crack this nut. One of the methods I used in the past was to send the whole input as a single attribute on the service call, and then pass it into db call (via PHP) using the REGEX feature of MySQL. Using that feature, it searched for any combination of value 1, or value 2 or value 1 and value 2, etc.
But let's attack it from the front end as you're doing. You have an input coming in as a combination of "#word1 #word2". So rather than doing regex summersaults to find the beginning and the end, how about simplifying things and doing a string.split() on the "#" character. You can take the resulting array of values, loop to create your service call string (don't forget to trim blank spaces) and send the call off.
The problem is definetely coming from your regex. The second you write a second word in your textarea (I'm assuming you're using a textarea), the if condition fails.
I'm not regex expert, however I have a js solution:
var keywords = $(this).val().split(' ').trim();
Get all the keywords this way, then loop through each one and check if it's a correct tag.
var obj = [];
keywords.forEach(function (content) {
var go = content.match(start);
var name = content.match(word);
//check to see if the searchword is valid.
//If it is we add it to the list and continue the loop.
//If not we ignore it and continue the loop.
//If you want to stop the loop when one searchword is wrong add "return false;"
if(go !== null && go.length > 0 && name !== null && name.length > 0 ){
obj.push(name);
}
});
if(obj.length > 0){
var dataString = JSON.stringify(obj);
//do ajax call here
}
I'm also assuming you're sending all your searchwords at once to your php file.
EDIT: Pushing all valid keywords into an array obj that will be later parsed as a JSON string.

How to pass multiple parameter on dynamic link function jquery function C#?

I have one c# web application in which put one link dynamically like,
if (oObj.aData[1] == '2') {
Id = oObj.aData[7];
Name = oObj.aData[2];
alert(Name);
return ' Show ';
//this is
}
function like,
function Show(id,name)
{
alert('calling');
}
but my function not calling.
Is any syntax error or anything else which I forgetting?
Please help.
You need to pass Name in quotes(''), with in quotes to be treated as string parameter. Otherwise they will be treated as JS variable which obviously you have not defined, You must be getting error 'example string' is not defined. in browser console.
return ' Show ';
Note: If Id is a string, also pass it in quotes('')
This may be of some help.
Starting from Firefox 34 and Chrome 41 you will be able to use an ECMAScript 6 feature called Template Strings and use this syntax:
String text ${expression}
Example:
var a = 5;
var b = 10;
console.log(`Fifteen is ${a + b}.`);
// "Fifteen is 15.
source: JavaScript Variable inside string without concatenation - like PHP

UltraEdit scripting using JavaScript and wildcards

I have a program using a combination of JavaScript and UltraEdit scripting. The program has an array of strings to search for in a file/tab. If found, it moves the corresponding lines to a new file/tab. When using exact match it works great.
However, my source values are not exact matches. The values in the file are ######-## where the values after the dash vary. I have the value up to the dash. I attempted to build the wildcard into the array values, and I've attempted to concatenate it to the .find function with no success. Any thoughts would be greatly appreciated.
Here is the code I'm executing as a script within UltraEdit. I've truncated the array from the 50 values it contained for the purpose of demonstration.
// Start at the beginning of the file
UltraEdit.activeDocument.top();
// Search string variable used for copying of lines
//DD011881 - Building an array of values
var delString = new Array()
delString[0] = "'99999999'";
delString[1] = "'169-*'";
delString[2] = "'5482-*'";
delString[3] = "'5998-*'";
delString[4] = "'36226-*'";
delString[5] = "'215021-*'";
// Array loop value
var x = 0;
var arrayLen = delString.length
// Start with nothing on the clipboard
UltraEdit.clearClipboard();
for (x=0; x<arrayLen; x++)
{
// Establish our search string for the loop condition
var bFound = false;
while (UltraEdit.activeDocument.findReplace.find(delString[x])){
UltraEdit.activeDocument.selectLine();
UltraEdit.activeDocument.copyAppend("^c" + "\n");
bFound = true;
}
UltraEdit.activeDocument.top();
if (bFound) {
UltraEdit.document[6].paste();
UltraEdit.activeDocument.top();
UltraEdit.clearClipboard();
}
} // For Loop
In your UltraEdit script you want to run an UltraEdit regular expression find in the while loop, but you have never set the regular expression engine nor any find parameter. So the script is executing the find with the internal defaults for finds (case-insensitive, non-regular expression search downwards, not matching whole words with the Perl regular expression engine selected).
Insert in your UltraEdit script below the command UltraEdit.clearClipboard(); the following lines:
UltraEdit.ueReOn();
UltraEdit.activeDocument.findReplace.mode = 0;
UltraEdit.activeDocument.findReplace.matchCase = true;
UltraEdit.activeDocument.findReplace.matchWord = false;
UltraEdit.activeDocument.findReplace.regExp = true;
UltraEdit.activeDocument.findReplace.searchDown = true;
if (typeof(UltraEdit.activeDocument.findReplace.searchInColumn) == "boolean") {
UltraEdit.activeDocument.findReplace.searchInColumn = false;
}
Now the UltraEdit regular expression is selected for the script and the find parameters are set for running a case-sensitive (faster) regular expression search.
And please remove "^c" + "\n" from command UltraEdit.activeDocument.copyAppend() as this command does not take any parameter. With the command above, the entire line including the line termination is already selected and this selection is appended to the clipboard, not the string you put into the parentheses of command copyAppend().

Categories

Resources