v2
- by intrnl 7/24/202200
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
function escape (value) {
	const str = '' + value;
	const res = str.replace(/[&<]/g, (char) => '&#' + char.charCodeAt(0) + ';');

	return res;
}

const ATTR_REGEX = /[&"]/g;
const TEXT_REGEX = /[&<]/g;

function escape_new (value, is_attr = false) {
	const str = '' + value;

	const pattern = is_attr ? ATTR_REGEX : TEXT_REGEX;
	pattern.lastIndex = 0;

	let escaped = '';
	let last = 0;

	while (pattern.exec(str)) {
		const idx = pattern.lastIndex - 1;

		escaped += str.substring(last, idx) + ('&#' + str.charCodeAt(idx) + ';');
		last = idx + 1;
	}

	return escaped + str.substring(last);
}

function escape_no_re (value, is_attr = false) {
	const str = '' + value;

	let escaped = '';
	let last = 0;

	for (let idx = 0, len = str.length; idx < len; idx++) {
		const char = str.charCodeAt(idx);

		if (char === 38 || char === (is_attr ? 34 : 60)) {
			escaped += str.substring(last, idx) + ('&#' + char + ';');
			last = idx + 1;
		}
	}

	return escaped + str.substring(last);
}
delete caserun single casemove downdrag and drop case


ready



escape('fooo<=bar&baz;');
delete caserun single casemove upmove downdrag and drop case


ready



escape_new('fooo<=bar&baz;');
delete caserun single casemove updrag and drop case


ready



escape_no_re('fooo<=bar&baz;');
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN