用回车替换字符 return - JavaScript
Replace characters with carriage return - JavaScript
我想寻求帮助学习如何从字符串中替换 3 种不同的字符组合,并用回车替换它们 return。
字符组合是:
++~
~~+
+~\
我希望能够用马车替换这些组合 return。
字符串示例:
Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\
如能提供代码示例方面的帮助,我们将不胜感激。
谢谢!
更新
我有一个入门函数,它可以工作,但我不确定这是否是一个可扩展的解决方案。
function findAndReplace() {
var string = 'Addendum and/or contract providing additional event details and conditions. Capacity for the King St. concerts is 3,645 persons with additional safety conditions as per Addendum.++~ Addendum and/or contract providing additional event details and conditions on file in Madison Parks.++~ Notification: Event participants must be notified prior to the race that they must adhere to the traffic signals. They are not allowed to stop traffic during the event.++~ Organizer must notify hotels, businesses and residents along the approved bike route. Include estimated time periods when athletics will "block" access and provide day-off contact information.++~ Call the Sayle Street Garage, 608-266-4767, 1120 Sayle St, to make arrangements to pick up and return barricades required for event. There may be charges for this equipment.++~ ';
var target1 = '++~ ';
var target2 = '~~+ ';
var target3 = '+~\ ';
var replacement = '\n';
var i = 0, length = string.length;
for (i; i < length; i++) {
string = string.replace(target1, replacement)
.replace(target2, replacement)
.replace(target3, replacement);
}
return string;
}
console.log(findAndReplace());
你可以尝试使用js中的replace函数:-
let sampleStr = `Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\ `;
let replacedString = sampleStr.replace(/\++~/g, '\r').replace(/~~\+/g,'\r').replace(/\+~\/g,'\r');
alert(replacedString);
这个简单的正则表达式将替换字符串中的所有出现。
/\+\+~|~~\+|\+~\/g
您首先需要对字符串中的 \
进行转义,这样 abc+~\monkey
就会变成 abc+~\monkey
.
然后就可以使用split来拆分items了。 map 对物品做一些清理,然后 join 插入你的马车 return \r\n
let str = 'Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\'
str = str.split(/\+\+~|~~\+|\+~\/g).map(i => i.trim()).join('\r\n')
console.log(str)
你可以试试这个
const str = "Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\ ";
console.log(str.replace(/\+\+~|~~\+|\+~\/g, '<new symbol>'));
我想寻求帮助学习如何从字符串中替换 3 种不同的字符组合,并用回车替换它们 return。
字符组合是:
++~
~~+
+~\
我希望能够用马车替换这些组合 return。
字符串示例:
Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\
如能提供代码示例方面的帮助,我们将不胜感激。
谢谢!
更新
我有一个入门函数,它可以工作,但我不确定这是否是一个可扩展的解决方案。
function findAndReplace() {
var string = 'Addendum and/or contract providing additional event details and conditions. Capacity for the King St. concerts is 3,645 persons with additional safety conditions as per Addendum.++~ Addendum and/or contract providing additional event details and conditions on file in Madison Parks.++~ Notification: Event participants must be notified prior to the race that they must adhere to the traffic signals. They are not allowed to stop traffic during the event.++~ Organizer must notify hotels, businesses and residents along the approved bike route. Include estimated time periods when athletics will "block" access and provide day-off contact information.++~ Call the Sayle Street Garage, 608-266-4767, 1120 Sayle St, to make arrangements to pick up and return barricades required for event. There may be charges for this equipment.++~ ';
var target1 = '++~ ';
var target2 = '~~+ ';
var target3 = '+~\ ';
var replacement = '\n';
var i = 0, length = string.length;
for (i; i < length; i++) {
string = string.replace(target1, replacement)
.replace(target2, replacement)
.replace(target3, replacement);
}
return string;
}
console.log(findAndReplace());
你可以尝试使用js中的replace函数:-
let sampleStr = `Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\ `;
let replacedString = sampleStr.replace(/\++~/g, '\r').replace(/~~\+/g,'\r').replace(/\+~\/g,'\r');
alert(replacedString);
这个简单的正则表达式将替换字符串中的所有出现。
/\+\+~|~~\+|\+~\/g
您首先需要对字符串中的 \
进行转义,这样 abc+~\monkey
就会变成 abc+~\monkey
.
然后就可以使用split来拆分items了。 map 对物品做一些清理,然后 join 插入你的马车 return \r\n
let str = 'Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\'
str = str.split(/\+\+~|~~\+|\+~\/g).map(i => i.trim()).join('\r\n')
console.log(str)
你可以试试这个
const str = "Capacity for the concerts is 3,645 persons with additional safety conditions.++~ Approved contractor will barricade and cone the race route.++~ Coordinate activities and schedule with the street coordinator, 608-261-9171.++~ Animals must remain in fenced area ~~+ Maintain access to Metro stops.~~+ There is no event parking in the parking lot.~~+ Event volunteers and staff will monitor the barricades during the event.~~+ Staff will review the event for compliance to the established conditions and determine what remediation (if any) is needed and/or establish considerations for future events.+~\ Event organizer/sponsor is responsible for cleanup of event area. Charges will be assessed for any staff time or resources required for clean-up.+~\ ";
console.log(str.replace(/\+\+~|~~\+|\+~\/g, '<new symbol>'));