简体   繁体   English

不了解如何在Angular中大写文本

[英]Not Understanding How to Capitalizing Text in Angular

My friend helped me write a custom filter in AngularJS to help me capitalize one of the values in my object for one of my arrays. 我的朋友帮助我在AngularJS中编写了一个自定义过滤器,以帮助我为数组之一大写对象中的值之一。 But didn't have time to explain to me what he did. 但是没有时间向我解释他的所作所为。 Was just wondering if anyone can be kind to help me understand this block of code: 只是想知道是否有人可以帮助我理解以下代码块:

.filter('capitalizetext', function() {
   return function(name) {
name = ( name === undefined || name === null ) ? '' : name;
return name.toString().toLowerCase().replace( /\b([a-z])/g, function(change) {
  return change.toUpperCase();
  });
 };
})

1st part that I don't understand is: 我不明白的第一部分是:

name = ( name === undefined || name === null ) ? '' : name;

Why did he do this? 他为什么要这样做?

2nd part I don't understand is: 我不明白的第二部分是:

return name.toString().toLowerCase().replace( /\b([a-z])/g,

I understand that he is changing the string to all lowercase so that he can eventually convert it to capitalize the string but what is this: ( /\\b([az])/g 我了解他正在将字符串更改为所有小写字母,以便他最终可以将其转换为大写字符串,但这是什么: ( /\\b([az])/g

Not really following what he did there. 没有真正遵循他在那所做的事情。

Please help! 请帮忙!

name = ( name === undefined || name === null ) ? '' : name;

This ensures that name is never null or undefined when using it later. 这样可以确保以后使用该name时,该name永远不会为null或未定义。

return name.toString().toLowerCase().replace( /\b([a-z])/g, function(change) {
  return change.toUpperCase();
  });
 };

First change everything to lowercase and replace the first character of EVERY word to the uppercase version. 首先将所有内容更改为小写,然后将每个单词的第一个字符替换为大写版本。 \\b is a boundary matcher : \\b是一个边界匹配器

Eg suppose name = "capItalIze me" 例如,假设name = "capItalIze me"

Then 然后

  1. name.toString().toLowerCase(); // => name = "capitalize me"
  2. /\\b([az])/g // means find first letter of every word so will match "c" and "m"
  3. replace( /\\b([az])/g, function(change) {return change.toUpperCase();});} // change 'c' to 'C' and 'm' to 'M';

In the first part: 在第一部分中:

name = ( name === undefined || name === null ) ? '' : name;

He checks to see if the string is truthy in his "definition", checking if it is undefined or null (as a null/undefined string could raise errors), else if it is, he sets it to an empty string to avoid errors. 他检查字符串在其“定义”中是否真实,检查字符串是否未定义或为null(因为null / undefined字符串可能会引发错误),否则,将其设置为空字符串以避免错误。

The second part he uses a regex expression to modify the string to the filter specification. 第二部分,他使用regex表达式来将字符串修改为过滤器规范。 You can read about regex expression here . 您可以在此处阅读有关regex表达式的信息 I am not well versed in regex, but I think you are on the right track when he converts all characters to lowercase , check the comment made above for the regex explanation, however, if this is the case, he could just do this... 我不太熟悉regex,但是当他将所有字符都转换为小写字母时,我认为您是对的 ,请检查上面针对regex的注释,但是,如果是这种情况,他可以这样做。 。

string = string.toLowerCase()
string = string.substr(0, 1).toUpperCase() + string.substr(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM