How do I insert a space/carriage return after a designated number of characters (eight) within a designated html field pulling from a table?
Example:
<ebs:PMLOAD TABLE='DS_CLAIM'><ebs:PMVAL FIELD='DS_CLAIM.CGROUPING_FIELD'></ebs:PMVAL></ebs:PMLOAD>
So if the field contains 12345678910, the output would read:
12345678
910
var arr = string.split(""), output;
arr.splice(8, 0, ' ');
output = arr.join('');
With HTML you don't.
JavaScript is the handler, Using replace method.
str = "1234567890";
alert(str.replace(/^(.{8})(.*)$/mg,'\1 \2'));
Related
I have a string where i want to split it based on the tags
Sample string:
var str = '<p>Hi my name is XXX</p>\n\n<p>Iam going to london</p></p>'
I want to split the string based on their tags
Expected result:
splitString = ['<p>Hi my name is XXX</p>', '<p>Iam going to london</p>'];
What regex should be used to get the expected result, thanks in advance!
An approach without a regular expression (see the comments for the "why").
Create a temporary container (.createElement())
Insert the markup and let the browser handle the parsing and fixing of maybe invalid markup (.insertAdjacentHTML())
Get the nodes you want (.querySelectorAll())
Convert the HTMLCollection into an actual array for easier handling (Array.from())
Get the .outerHTML (.map())
function getParagraphs(htmlString) {
const div = document.createElement("div");
div.insertAdjacentHTML("beforeend", htmlString);
return Array.from(div.querySelectorAll("p"))
.filter(p => p.textContent !== "") // because of the lonely </p> at the end - optional
.map(p => p.outerHTML);
}
const str = '<p>Hi my name is XXX</p>\n\n<p>Iam going to london</p></p>';
console.log(getParagraphs(str));
var str = '<p>Hi my name is XXX</p>\n\n<p>Iam going to london</p></p>'
console.log( str.match(/<p>([^\<]*?)<\/p>/g) );
see: https://regex101.com/r/mtPZVg/1
I'm trying to build an html form that generates structured JSON code from the input values. This form uses repeat fields.
I have to modify name attributes in cloned fields.
In Javascript I have a string 'menu-item[0][offers][0][price]'.
I would like to replace 'menu-item[0]' with 'menu-item[1]', as in example at http://regexr.com/3gpnt
I'm using RegExp, but is not required.
This is my experiment, but it doesn't work.
var string = 'menu-item[0][offers][price]';
var itemName = 'menu-item';
var regExp = new RegExp(itemName + '\[(.*?)\]', "");
var newString = string.replace(regExp, itemName + '[1]');
console.log(newString);
alert(newString);
Returns 'menu-item[0][offers][price]'.
Test on jsfiddle https://jsfiddle.net/lorenzodetomasi/dmnd7f9L/
Thank you.
I have the below input
var input = (a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]
I need to remove the comma within the square brackets such that the final output will be
var output = (a-d){12-16},(M-Z){5-8},[#$%!^12+-23^!]
By solution
function test()
{
var input = '(a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]'; //input string
var splitByFirstBracket = input.split("["); //split the input by [ character
//merge the arrays where the second array is replaced by '' for ','
var output = splitByFirstBracket[0] + '[' + splitByFirstBracket[1].replace(/,/g,'');
alert(output);
}
It is providing the output correctly. Is there any better way - I am open both for JavaScript and JQuery.
Thanks in advance
You can use a regular expression replacement. The replacement can be a function, which receives the part of the input that was matched by the regexp, and then it can calculate the replacement. In this case, it would use another replace call to remove the commas.
var input = '(a-d){12-16},(M-Z){5-8},[#$%!^,12+-,23^!]'; //input string
var output = input.replace(/\[.*?\]/g, function(match) {
return match.replace(/,/g, '');
});
console.log(output);
How to cut text using jQuery? For example :
if output like :
new/2016/songs1.mp3
new/2015/songsx.mp3
new/songs3.mp3
Need output :
songs1.mp3
songsx.mp3
songs3.mp3
I want to put only file name with extension like songs-name.mp3 not directory, so i want to cut this using jQuery.
split it and take the last item
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
alert(items[items.length - 1 ]);
or simply
alert( str.split("/").pop() );
if you want to remove the rest of the text then
var str = "new/2016/songs1.mp3";
var items = str.split( "/" );
str = items[items.length - 1 ];
or
str = str.split("/").pop();
Check it out:
Here i use split function to split the string and then its return an array.
From that array we have to get the last portion i.e, Song name, So we have to get the length of that array.
After that we alert the array with the index of last portion.
var str = "new/2016/songs1.mp3";
var arr = str.split("/");
alert(arr[(arr.length) - 1]);
try this
var filepath = "new/2015/songsx.mp3";
console.log(filepath.slice(filepath.lastIndexOf("/")+1));
Using the slice method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice) you can get a part of a string.
Using the lastIndexOf method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf) returns the index of the last occurrence of the given string (or -1 if given string does not occur in the string).
So what we are doing is getting the slice if the file path starting with the first character after the las "/" in the filepath variable.
I need to do some functions on some text field contents before submitting the form, like checking the validity of the customer registeration code, having the customer name as his code in customers table appending an incrementing number.
I don't want to do it after the form is submitted becuase I need it to be displayed in the code field before submitting the form.
My code:
function getCode(){
var temp = document.getElementById("temp").value ;
var d = parseInt(document.getElementById("temp").value) + 1;
document.getElementById("customernumber").value = d;
document.getElementById("code").value = document.getElementById("name").value+"-"+ d;
}
It all works fine, but the last line of code developed the code WITH the spaces between the code.
A couple ways to remove spaces...
Using regex: string.replace(/ /g,'');
Splitting the string by spaces and combining the array with no delimiter:
string.split(' ').join('');
var str = "ab cd ef gh ";
str = str.replace(/\s+/g,"");