Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to parse this inbound email body_text so that is looks like "Employee Start Date":" 2021/09/07"
I'm trying to put the string through a exReg, but can't get it to format correctly. Any suggestions? I have recently updated my script.
thanks!
Employee Start Date:
2021/09/07
Employee ID:
123456789
Employee Preferred Name:
John Doe
Employee Company:
Global
var emailObj = {};
try {
var valPairs = body_text;
var regEx = /^(.+):(.+)$/; //Name is everything preceding first :, value everything after.
var valPairs = body_text.split("\n").filter(function (item) {
return regEx.test(item);
});
var matches;
//loop through email body and build object
for (var i = 0; i < valPairs.length; i++) {
matches = valPairs[i].match(regEx);
try {
emailObj[matches[1].toString().trim()] = matches[2].toString().trim(); //Add trimmed name/val to object
} catch (ex) {
gs.error(ex.message);
}
}
} catch (ex) {
gs.error(ex.message);
}
console.log(emailObj)
The name value pairs needs to be on on the same line with no line breaks. I need the results to look like this.
Employee Start Date: 2021/09/07
Employee ID: 123456789
Employee Preferred Name: John Doe
Employee Company: Global
thanks in advance!
Here's an example to format the html better. It also parses it from the JSON string into a jS object.
const email = "Employee Start Date:<br> <br> 2021/09/07 <br> <br> Employee ID:<br> <br> 123456789 <br> <br> Employee Preferred Name:<br> <br> John Doe <br> <br> Employee Company: <br> <br> Global<br> <br>"
function extractData(str) {
str = str.replace(/(:\s*(?:<br>\s*)+)/gm, '": "');
str = str.replace(/(\s*(?:<br>\s*)+)/gm, '", "');
str = str.replace(/(,\s*"$)/gm, '}');
str = str.replace(/(^)/gm, '{"');
return JSON.parse(str)
}
console.log(extractData(email))
Related
I want to take a string in this format I am a level ${level} coder, where ${level} will be some value passed in. But I want only specific word in this sentence bolded. So lets say in this example I want "level" and "coder" bolded. How do I achieve this?
Current Behavior:
Even if I do <b> or <strong> inside `` the tags just get converted to string. It doesn't actually bold the text for me.
Update: This is exactly what I am doing with aws sns. But I want to achieve this with string interpolation.
let snsData = {
Message: < strong > "This is an automated message" < /strong> + '\n' +
"You have successfully uploaded the following:" + '\n'
`File name: ${snsFileName}\n
Number of lines: ${numberOfLines}\n
If there are any issues, please contact XXX for assistance.`,
Subject: 'Successfully Uploaded to XX',
TopicArn: 'XXXXX'
};
Addendum, so this is entirely unique to your instance and I suggest better familiarizing yourself with how string / interpolation and objects work but for the sake of learning, cheers;
const $ = function(id) { return document.getElementById(id) },
level = 'expert',
str = `I am a level <strong>${level} coder</strong>`,
snsFileName = 'testFileNameBlah',
numberOfLines = 99,
snsData = {
Message: '<strong>This is an automated message</strong><br/>' +
'You have successfully uploaded the following:<br/>' +
`File name: <strong>${snsFileName}</strong><br/>
Number of lines: <strong>${numberOfLines}</strong><br/>` +
'If there are any issues, please contact XXX for assistance.<br/>',
Subject: 'Successfully Uploaded to XX',
TopicArn: 'XXXXX'
};
$('blah').innerHTML = str + '<hr>';
$('fixme').innerHTML = snsData.Message + snsData.Subject;
<span id="blah"></span>
<h2>Addendum</h2>
<p id="fixme"></p>
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I would like to force the input value of a text field to comply with a specific format. It needs to be capitalized and with no numbers allowed.
I would like this event to fire on onKeydown().
Examples:
Lionel MESSI => Lionel Messi
LiONEL MesSI => Lionel Messi
Neymar JR => Neymar Jr
Neymar 22 JR => Neymar Jr
Franck D'HONNEUR => Franck D'Honneur
Kevin PEREZ ROBERTO => Kevin Perez Roberto
There is no simple one-liner way of doing this. However, you can make a function which can format the name for you.
Originally taken from this answer, I've modified it slightly to reflect your desired output:
var name1 = "Lionel MESSI";
var name2 = "LiONEL MesSI";
var name3 = "Neymar JR";
var name4 = "Neymar 22 JR";
var name5 = "Franck D'HONNEUR";
var name6 = "Kevin PEREZ ROBERTO";
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}).replace(/[0-9]/g, '');
}
console.log(toTitleCase(name1));
console.log(toTitleCase(name2));
console.log(toTitleCase(name3));
console.log(toTitleCase(name4));
console.log(toTitleCase(name5));
console.log(toTitleCase(name6));
You may want to check out toUpperCase() and toLowerCase() from the MDN documentation.
To get this functionality in on onKeyDown(), you can use jQuery like the snippet below. I do advice against onKeyDown() though as this creates a weird user experience. Try onBlur instead.
function toTitleCase(str) {
return str.replace(/\w\S*/g, function(txt){
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}).replace(/[0-9]/g, '');
}
$("#name").on("keydown", function() {
$(this).val(toTitleCase($(this).val()));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Name: <input id="name">
With the help of a little helper function from this question: How do I make the first letter of a string uppercase in JavaScript?
I wrote a quick script that fires on input blur:
$(document).ready(function() {
function capitalizeFirstLetterOfEachWord(string) {
strArr = string.split(' ');
resArr = [];
for(i=0;i<strArr.length;i++){
resArr[i] = strArr[i].charAt(0).toUpperCase() + strArr[i].slice(1);
}
return resArr.join(" ");
}
$('.forceCapital').blur(function(e) {
$(this).val(capitalizeFirstLetterOfEachWord($(this).val()));
});
});
https://jsfiddle.net/sa2ox30d/2/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Is there a function in Java Script that divide a string with several arguments?
var string = "This is a test text, again, this is a testing text";
For example, I can split with , by saying string.split(','); and I will have:
var string = ["This is a test text", "again", "this is a testing text"];
Now, I want to split it with several parameters, so string now would be
var string = ["test text", "testing text"]
I'm looking for a function that extract all the parts that start with test and end with text.
I'm not sure that I understood what you want, but here is a function I wrote in 2 minutes. With the following scenario
var string = "This is a test text, again, this is a testing text";
function customSplit(str_to_split, start_string, end_string) {
var res = [];
var start_index, end_index;
for (i = 0; i <= str_to_split.length; i++) {
start_index = str_to_split.toLowerCase().indexOf(start_string.toLowerCase(), i);
if (i == start_index) {
end_index = str_to_split.toLowerCase().indexOf(end_string.toLowerCase(), i);
if (end_index >= 0) {
res.push(str_to_split.substring(start_index, end_index + end_string.length));
}
}
}
return res;
}
console.log(customSplit(string, "test", "text"));
it will output ["test text", "testing text"].
Let me know if it helps you.
EDIT
Corrected a wrong behave with a particular string. Please remind that I wrote it in a couple of minutes.
Use a regex:
var str = "This is a test text, again, this is a testing text";
console.log(str.match(/test.+?text/g));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need your help on my new project
I hate regular expressions and its rules but i must use it this project.
want do this replace
var aString = '[bkz:sample key]';
I want get into key variable 'sample key' value from this aString
var key,clean;
key = 'sample key';
clean = cleanChars(key);
// clean = sample_key
//my target
key
how can i do this?
thanks in advance
function extractKey(str) {
var match = (str || '').match(/^\[bkz:(.+)\]$/);
return match? match[1] : '';
}
extractKey('[bkz:sample key]'); //sample key
var aString = "[bkz:sample key]";
var regex = /^\[(.+)\:(.+)\]$/;
var matches = regex.exec(aString);
// "sample key" should now be in matches[2]
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I currently have a JSON encoded string generated by inputting values from a array, it is as follows -
"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"
I need to convert it to the following format :
"[{value: 97049},{value: 84866},{value: 39402},{value: 30250},{value: 33363}]"
Thanks.
$input = $json_var;
$input = str_replace( '"', '', $input ); // strip em
$input = '"' . $input . '"'; // wrap back around
JS:
var json_array = JSON.parse(json_string);
for (var i = 0; i < json_array.length; i++) {
json_array[i] = JSON.parse(json_array[i];
}
PHP:
$json_array = json_decode($json_string);
$json_array = array_map('json_decode', $json_array);
It would probably be better to fix this at the source. If it's supposed to be an array of objects, don't quote each array element before adding them to the array.
var myQuotedJson = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
var myUnquotedJson = myQuotedJson.replace(/"/, '');
You can do that like this:
var input = '"["{value: 97049}","{value: 84866}","{value: 39402}","{value: 30250}","{value: 33363}"]"';
output = '"' + input.replace('"','') + '"';
//Alerts your output
alert(output );